如此,代码如下
<?php
header("Content-type:text/html;charset=utf-8");
//call
class Monkey{
public $name;
protected $food;
public function __construct($name,$food){
$this->name = $name;
$this->food = $food;
}
public function __call($method,$args){
return $this->$args[0]($args[1], $args[2]);
}
//成员函数
public function getSum($n1, $n2){
return $n1 + $n2;
}
}
$monkey = new Monkey('妖猴','兔子');
$res = $monkey->play('getSum',200,400);
echo $res;
?>
执行后出现
Notice: Array to string conversion in D:\phptools\Apache24\htdocs\oop\call1.php on line 15
Notice: Undefined property: Monkey::$Array in D:\phptools\Apache24\htdocs\oop\call1.php on line 15
Fatal error: Uncaught Error: Function name must be a string in D:\phptools\Apache24\htdocs\oop\call1.php:15 Stack trace: #0 D:\phptools\Apache24\htdocs\oop\call1.php(25): Monkey->__call('play', Array) #1 {main} thrown in D:\phptools\Apache24\htdocs\oop\call1.php on line 15
此处,$args已是一个数组($args[0]=getSum, $args[1]=200,$args[2]=400)
1,为什么出现 Undefined property: Monkey::$Array?
2, $this->$args0;这样调用成员函数为什么会不成功呢?
程序应该是这样执行后求出和才对啊,$this->getSum(200,400);