本篇文章給大家帶來的內容是關于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);
- }
- //Vevb.com
- }
- $obj = new StaticDemo(array(new One(), new Two()));
- $obj->method_1();
- $obj->method_3();
運行結果:11,33
新聞熱點
疑難解答