Ilija,PHP< 5.3没有“
Late Static Binding”,这就是为什么你可能遇到没有被调用的FooPage :: displayContent.如果您运行的是PHP 5.2,则没有什么可做的(除了
some hacks using debug_backtrace(),老实说我不建议这种情况).
现在,它真正引起我注意的是你的方法都是静态的;是否有一个原因?他们为什么不是实例方法?我希望有类似的东西:
include_once(kContent . "HeaderContent.class.php");
include_once(kContent . "HeaderContent.class.php");
abstract class Page
{
protected $header;
protected $footer;
public function __construct()
{
$this->header = new HeaderContent();
$this->footer = new FooterContent();
}
public function display()
{
$this->displayHeader();
$this->displayContent();
$this->displayFooter();
}
public function displayContent()
{
print "
some content
";}
public function displayHeader()
{
$this->header->display();
}
public function displayFooter()
{
$this->footer->display();
}
};
class FooPage extends Page
{
public function displayContent()
{
print "
Foo page
";}
}
以后在你的视图中你会写如下:
$page = new FooPage();
$page->display();
有些事情需要考虑:
>通常最好不要在生成视图内容时使用print / echo.而是尝试创建字符串并作为最后一步执行print / echo.这使得以后编写测试更容易.
例:
public function display()
{
return
$this->displayHeader() .
$this->displayContent() .
$this->displayFooter();
}
public function displayContent()
{
return "
some content
";}
public function displayHeader()
{
return $this->header->display();
}
....
$page = new FooPage();
echo $page->display();
>如果您需要在应用程序增长时执行此操作,则可以将页眉和页脚作为Page构造函数参数传递.只要它们是理解display()消息的对象(即polymorphic),就应该没问题.
HTH