本篇文章給大家帶來的內容是關於php如何使用_call實現多繼承(代碼示例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
這篇文章簡單介紹下使用_call實現代碼的復用。
_call:php的一個魔術方法,當調用類中不存在的method時,會自動調用_call.
示例代碼:
class One{
function method_1(){
echo '11
';
}
function method_2(){
echo '22
';
}
}
class Two{
function method_3(){
echo '33
';
}
function method_4(){
echo '44
';
}
}
class StaticDemo{
protected $Class = array();
public function __construct(array $class = array()){
$this->Class = $class;
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
foreach ($this->Class as $v){
if (is_callable(array($v, $name))) {
//call_user_func_array在上篇文章中已作出理解
return call_user_func_array(array($v, $name), $arguments);
}
}
return call_user_func_array(array($this, $name), $arguments);
}
}
$obj = new StaticDemo(array(new One(), new Two()));
$obj->method_1();
$obj->method_3();
運行結果:11,33
以上就是php如何使用_call實現多繼承(代碼示例)的詳細內容,更多請關注其它相關文章!
更多技巧請《轉發 + 關注》哦!