Home > Article > Backend Development > How to use __call() and __callStatic() in PHP
This article mainly shares with you how to use __call() and __callStatic() in PHP. I hope it can help you. I won’t explain too much about the use of these two methods. Through the example code and results, everyone can understand the functions of the two more clearly.
1.__call() method. When calling a method that is not declared in the class, you can call the __call() method instead of declaring a method. Accepts method name and array as parameters.
Code example:
<?php class test{ //魔术方法__call /* $method 获得方法名 $arg 获得方法的参数集合 */ public function __call($method,$arg){ echo '你想调用我不存在的方法',$method,'方法<br/>'; echo '还传了一个参数<br/>'; echo print_r($arg),'<br/>'; } $list=new test(); $list->say(1,2,3); ?>
Execution result:
You want to call my non-existent method say method
also passed it One parameter
Array ([0] => 1 [1] => 2 [2] => 3 )
<?php class test{ //魔术方法__callStatic /* $method 获得方法名 $arg 获得方法的参数集合 */ //魔术方法__callStatic public static function __callStatic($method,$arg){ echo '你想调用我不存在的',$method,'静态方法<br/>'; echo '还传了一个参数<br/>'; echo print_r($arg),'<br/>'; } } test::cry('痛哭','鬼哭','号哭'); ?>Execution result:
You want to call my non-existent cry static method
Also passed a parameter
Array ([0] => Cry[1] => Ghost cry[2] => Cry)
How to use the __call() method in php and overloading example analysis
php magic function __call() usage example analysis_PHP tutorial
Detailed explanation of php's magic methods __get(), __set(), __call(), __callStatic() and static usage
The above is the detailed content of How to use __call() and __callStatic() in PHP. For more information, please follow other related articles on the PHP Chinese website!