当程序中调用未定义的静态方法时,__callStatic()方法会被调用。
__callStatic()的用法与__call()类似。示例如下:
<?php
class Person
{
function say()
{
echo "Hello, world!<br>";
}
public static function __callStatic($funName, $arguments)
{
echo "The static method you called:" . $funName . "(parameter:" ; // 输出不存在的方法的名称
print_r($arguments); // 输出不存在的方法的参数列表
echo ")does not exist!<br>\n";
}
}
$Person = new Person();
$Person::run("teacher"); // 如果对象内不存在的方法被调用,则 __callStatic() 方法会被自动调用
$Person::eat("John", "apple");
$Person->say();
输出结果如下:
The static method you called: run (parameter: Array([0] => teacher)) does not exist! The static method you called: eat (parameter: Array([0] => John[1] => apple)) does not exist! Hello world!
登录后可发表评论