Php多类扩展一
可以扩展php类(多个类扩展一个类) -----init.php------ -----Functions.php------ -----Template.php------ -----debug.php------ 如果你知道更好的方法,请与我分享。Php多类扩展一,php,function,class,object,extends,Php,Function,Class,Object,Extends,可以扩展php类(多个类扩展一个类) -----init.php------ -----Functions.php------ -----Template.php------ -----debug.php------ 如果你知道更好的方法,请与我分享。 (这些类位于“类函数”文件包含的不同文件中)当然可以。事实上,你可以用以下方法测试自己: 您可以包含对超类(在您的例子中是函数)不做任何操作的函数setTitle()。然后在模板中,您可以覆盖其功能。在这种情况下,在“debug”类上运行此函数是
(这些类位于“类函数”文件包含的不同文件中)当然可以。事实上,你可以用以下方法测试自己:
您可以包含对超类(在您的例子中是函数)不做任何操作的函数setTitle()。然后在模板中,您可以覆盖其功能。在这种情况下,在“debug”类上运行此函数是安全的。即
----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
public function setTitle($title){}
}
-----Template.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');
如果你真的想伪造多重继承,你可以使用神奇的函数\uu call()。
许多类可以扩展同一个类。类不能扩展多个。更好的方法是什么?这是无效的代码,并且没有对所需内容的描述。另外,类函数
有一种非常糟糕的代码味道。只是要指出:“类不能扩展多个”,但类可以使用许多特性(PHP5.4)。但是,我不推荐这种方法。只有当你知道自己在做什么时才使用它。
class Functions{
// content here
}
class Template extends Functions{
public $sTitle = "";
public function setTitle($title){$this->sTitle = $title;}
}
class debug extends Functions{
// content here
}
Functions:Template->setTitle('my title');
<?php
class SuperClass {
public function __construct(){ echo 'I am the common superclass. '; }
}
class LittleClass extends SuperClass{}
class LittleLittleClass extends SuperClass{}
new LittleClass();
new LittleLittleClass();
----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
public function setTitle($title){}
}
-----Template.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');