<?php
class Test{
public function hello(){
echo 'hello ';
}
protected function helloWorld(){
echo 'im protected helloWorld';
}
public function __call($method, $args){
echo '<br />';
echo "call function {$method}:".json_encode($args);
}
public static function __callStatic($method, $args){
echo '<br />';
echo "call static function {$method}:".json_encode($args);
}
}
$test = new Test();
$test->hello();
$test->helloWorld('hello', ['world','!']);
$test::helloEarth('hello', ['world','!']);
在Test类中,hello()方法是public,new一个对象就可以访问,所以,可以看到$test->hello()能够直接输出hello
。
helloWorld()方法是protected,是受保护的,只有继承的子类可以访问。当我们调用$test->helloWorld(‘hello’, [‘world’,’!’])时,发现输出的不是im protected helloWorld
,这是因为受到权限控制,转到__call()方法中了。
__callStatic()与__call()相似,静态调用不存在的方法或者受到权限控制时调用。