我有以下两个类。
class Settings
{
function __CONSTRUCT()
{
echo "Settings Construct";
}
}
class PageManager extends Settings
{
function __CONSTRUCT()
{
echo "PageManager Construct";
}
}
$page = new PageManager();
我认为这会很好,但它只运行 PageManager 的构造函数。我假设这是因为我重写了设置的构造函数。有什么方法我也可以调用父级的构造函数吗?
最佳答案
只需使用 parent::调用它
/* Settings */
class Settings{
function __CONSTRUCT(){
echo "Settings Construct";
}
}
/* PageManager */
class PageManager extends Settings{
function __CONSTRUCT(){
parent::__CONSTRUCT();
echo "PageManager Construct";
}
}
关于php - 在 PHP 中调用父级的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3681605/