Home > Article > Backend Development > Detailed explanation of how to use PHP__call and __callStatic
This article mainly introduces the __call and __callStatic methods of PHP magic methods. Friends in need can refer to them. I hope it can help you.
Core code
//魔术方法__call /* $method 获得方法名 $arg 获得方法的参数集合 */ class Human { private function t(){ } public function __call($method,$arg){ echo '你想调用我不存在的方法',$method,'方法<br/>'; echo '还传了一个参数<br/>'; echo print_r($arg),'<br/>'; } public static function __callStatic($method,$arg){ echo '你想调用我不存在的',$method,'静态方法<br/>'; echo '还传了一个参数<br/>'; echo print_r($arg),'<br/>'; } } $ha = new Human(); //example1 $ha->t(1,2,3); echo '<br>'; //example2 $ha->say('a','b','c'); echo '<br>'; //example3 $ha::run('d','e','f');
You want to call my non-existent method t method
Also passed a parameter
Array ( [0] => 1 [1] => 2 [2] => 3 )
You want to call my non-existent method say method
Also passed a parameter
Array ( [0] => a [1] => b [2] => c )
You want to call my non-existent run static method
You also passed a parameter
Array ( [0] => d [1] => e [2] => f )
Related recommendations:
php’s magic method __get() , __set(), __call(), __callStatic() and static usage detailed explanation
Detailed explanation of the usage and difference between __call and __callStatic in php
The above is the detailed content of Detailed explanation of how to use PHP__call and __callStatic. For more information, please follow other related articles on the PHP Chinese website!