本篇文章给大家带来的内容是关于php如何使用_call实现多继承(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
这篇文章简单介绍下使用_call实现代码的复用。
_call:php的一个魔术方法,当调用类中不存在的method时,会自动调用_call.
示例代码:
class One{
function method_1(){
echo '11<br/>';
}
function method_2(){
echo '22<br/>';
}
}
class Two{
function method_3(){
echo '33<br/>';
}
function method_4(){
echo '44<br/>';
}
}
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免费学习笔记(深入)”;
以上就是php如何使用_call实现多继承(代码示例)的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号