回顾php魔术方法__call(),__callStatic()
<?phpdeclare(strict_types=1);//开启强类型模式//不可访问的方法:private/protected/不存在的方法classPerson{publicfunctionsay(){echo"Helloworld";echo"\r\n";}}(newPerson())->say();//调用类中存在的方法(newPerson())->eat('food');//调用类中不可访问的方法
调用类中不存在的方法PHPFatalerror:UncaughtError:CalltoundefinedmethodPerson::eat()in/home/zrj/www/zhangrenjie_test/test/36.php:26Stacktrace:#0{main}thrownin/home/zrj/www/zhangrenjie_test/test/36.phponline26
classPerson{publicfunctionsay(){echo"Helloworld";echo"\r\n";}//在对象中调用一个不可访问方法时,__call()会被调用。publicfunction__call($functionName,$arguments){echo"您调用了类中不存在的方法:".$functionName."\r\n";echo"接受的参数为:".print_r($arguments,true);}}(newPerson())->say();(newPerson())->eat('food','chicken','bull');
Hello world
您调用了类中不存在的方法:eat
接受的参数为:Array
(
[0] => food
[1] => cocal
[2] => bull
)
classPerson{publicfunction__call(string$name,array$arguments){echo"Callnotexistsdynamicmethod:".$name."\r\n";echo$name.":".$arguments[0]."\r\n\r\n";}/**PHP5.3.0之后版本*/publicstaticfunction__callStatic(string$name,array$arguments){echo"Callnotexistsstaticmethod:".$name."\r\n";echo$name.":".$arguments[0]."\r\n\r\n";}}(newPerson())->say('helloworld');(newPerson())->__call('say',['helloworld']);Person::do('codingphp');Person::__callStatic('do',['codingjava']);
Call not exists dynamic method :say
say : hello world
Call not exists dynamic method :say
say : hello world
Call not exists static method :do
do : coding php
Call not exists static method :do
do : coding java
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。