0
这是一个被问了很多次的问题,但没有一个答案似乎适用于此。覆盖父功能
我有以下PageBase
类:
<?php
class PageBase {
public $templatefile = '404';
function Before() {}
function After() {}
function Display() {
$smarty = System::GetSmarty();
$smarty->display('views/'.$this->templatefile.'.tpl');
}
function Run() {
self::Before();
self::Display();
self::After();
}
}
所有这个类的做的就是为我的其他网页的基础;这些页面可能或不需要在显示页面之前/之后运行自己的代码,并且它们通常都有自己的$templatefile
。例如我的Home
类;
<?php
class Home extends PageBase {
public $templatefile = 'home';
function Before() {
var_dump($_REQUEST);
}
function After() {}
}
我遇到的问题是,正确的模板文件(home
)被加载,但在Home::Before()
代码不通过实例$home->Before()
跑,PageBase::Before()
改为跑了。
解决此问题的正确方法是什么? PHP提供任何方法来覆盖父级的功能吗?
亲切的问候
请参阅[后期静态绑定](http://php.net/manual/en/language.oop5.late-static-bindings.php)。或者更好的是,不要使用静态。即,而不是'self :: Before();'使用'$ this-> Before();'。 –
@AlexHowansky谢谢,不知道我是如何错过的,修复它! – Ieuan