这篇文章简朴引见下运用_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怎样运用_call完成多继续(代码示例)的细致内容,更多请关注ki4网别的相干文章!