call_user_func函数

<?php
  2     class Person
  3     {
  4         private $name;
  5         private $age;
  6         function __construct($name,$age)
  7         {
  8             $this->age = $age;
  9             $this->name = $name;
 10         }
 11
 12         function getInfo()
 13         {
 14             return [$this->name,$this->age];
 15         }
 16     }
 17
 18     class Man
 19     {
 20         private $person;
 21         function __construct($person)
 22         {
 23             $this->person = $person;
 24         }
 25         function __call($method, $args){
 26             echo get_called_class().PHP_EOL;
 27             return call_user_func([$this->person,$method],...$args);
 28         }
 29     }
 30     $person = new Person('tom',11);
 31
 32     $man = new Man($person);
 33     $result = $man->getInfo();
 34     var_dump($result);//通过回调函数调用对象方法 (用于=>调用某些接口自带的方法 直接采用__call来间接调用对象方法)
~                                

猜你喜欢

转载自www.cnblogs.com/hiraeth/p/9026396.html
今日推荐