【发布时间】:2014-08-22 13:43:18
【问题描述】:
可以扩展php类(多个类扩展一个)。
--init.php------
include_once $_SERVER['DOCUMENT_ROOT'].'/lib/php/functions.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/lib/php/template.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/lib/php/debug.php';
-----Functions.php-----
class Functions{
// content here
}
-----模板.php------
class Template extends Functions{
public $sTitle = "";
public function setTitle($title){$this->sTitle = $title;}
}
--debug.php------
class debug extends Functions{
// content here
}
Functions:Template->setTitle('my title');
如果您知道更好的方法,请与我分享。 (这些类位于“类函数”文件包含的不同文件中)
【问题讨论】:
-
如果真的想伪造多重继承,可以使用魔术函数
__call(). -
许多类可以扩展同一个类。类不能扩展多个。
-
一个更好的方法来做什么?这是无效的代码,并且没有您所期望的描述。此外,
class Functions的代码味道非常很糟糕。 -
只是指出:“类不能扩展多个”,但类可以使用许多特征(PHP 5.4)。但是,我不推荐这种方法。仅当您知道自己在做什么时才使用它。
标签: php function class object extends