PHP中把以两个下划线__开头的方法称为魔术方法(Magic methods),这些方法在PHP中充当了举足轻重的作用。
相关:《PHP中__construct(),类的构造函数详解》《PHP中__destruct(),类的析构函数》《PHP中__call()方法详解》
PHP中__callStatic(),用静态方式中调用一个不可访问方法时调用
此方法与上面所说的 __call() 功能除了 __callStatic() 是未静态方法准备的之外,其它都是一样的。
请看下面代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php
class Person
{
function say()
{
echo "Hello, world!<br>" ;
}
public static function __callStatic( $funName , $arguments )
{
echo "你所调用的静态方法:" . $funName . "(参数:" ;
print_r( $arguments );
echo ")不存在!<br>\n" ;
}
}
$Person = new Person();
$Person ::run( "teacher" );
$Person ::eat( "小明" , "苹果" );
$Person ->say();
|
登入後複製
运行结果如下:
1 2 3 | 你所调用的静态方法:run(参数:Array ( [0] => teacher ) )不存在!
你所调用的静态方法:eat(参数:Array ( [0] => 小明 [1] => 苹果 ) )不存在!
Hello, world!
|
登入後複製