该方法有两个参数。第一个参数$function_name自动接收未定义方法的名称,第二个参数$arguments以数组的方式接收该方法调用的多个参数。
__call()方法的用法
function __call(string $function_name, array $arguments)
{
// 方法体
}
程序中调用未定义的方法时,__call()方法会自动被调用。
示例如下:
<?php
class Person
{
function say()
{
echo "Hello, world!<br>";
}
function __call($funName, $arguments)
{
echo "The function you called:" . $funName . "(parameter:" ; // 输出不存在的方法的名称
print_r($arguments); // 输出不存在的方法的参数列表
echo ")does not exist!!<br>\n";
}
}
$Person = new Person();
$Person->run("teacher"); // 如果对象内不存在的方法被调用,则 __call() 方法会被自动调用
$Person->eat("John", "apple");
$Person->say();
输出结果如下:
The function you called: run (parameter: Array([0] => teacher)) does not exist! The function you called: eat (parameter: Array([0] => John[1] => apple)) does not exist! Hello world!
登录后可发表评论