【发布时间】:2013-02-28 04:44:13
【问题描述】:
我正在使用抽象的Page 类在 PHP 中创建模板系统。我网站上的每个页面都是它自己的类,扩展了Page 类。由于无法实例化像$page = new Page(); 这样的抽象类,我无法弄清楚如何在不知道该页面的类名的情况下实例化扩展页面的类。
如果我在运行时只知道抽象类的名称,是否可以实例化扩展抽象类的类?如果是这样,我将如何去做?
Page类的伪代码:
<?php
abstract class Page{
private $request = null;
private $usr;
function __construct($request){
echo 'in the abstract';
$this->request = $request;
$this->usr = $GLOBALS['USER'];
}
//Return string containing the page's title.
abstract function getTitle();
//Page specific content for the <head> section.
abstract function customHead();
//Return nothing; print out the page.
abstract function getContent();
}?>
加载所有内容的索引页面的代码如下:
require_once('awebpage.php');
$page = new Page($request);
/* Call getTitle, customHead, getContent, etc */
各个页面如下所示:
class SomeArbitraryPage extends Page{
function __construct($request){
echo 'in the page';
}
function getTitle(){
echo 'A page title!';
}
function customHead(){
?>
<!-- include styles and scripts -->
<?php
}
function getContent(){
echo '<h1>Hello world!</h1>';
}
}
【问题讨论】:
-
get_parent_class- php.net/manual/en/function.get-parent-class.php -
这得到了父母。我知道父类——Page——但我不知道子类的名称。
-
你能提供一些伪代码吗?由于某种原因没有完全遵循
-
您可以查看
Factory Method模式。所以你最终会得到类似Page::create('page1')的东西,它返回处理你网站的Page1的类的有效实例。 en.wikipedia.org/wiki/Factory_method_pattern -
很难做到这一点。也许你可以改变你的架构。向我们提供更多信息:您为什么要这样做?
标签: php class abstract-class instantiation