PHP__call与__callStatic使用方法详解

小云云
小云云 原创
2023-03-19 20:50:01 1140浏览

本文主要和大家介绍PHP魔术方法之__call与__callStatic方法,需要的朋友可以参考下,希望能帮助到大家。

核心代码


//魔术方法__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');

你想调用我不存在的方法t方法
还传了一个参数
Array ( [0] => 1 [1] => 2 [2] => 3 )

你想调用我不存在的方法say方法
还传了一个参数
Array ( [0] => a [1] => b [2] => c )

你想调用我不存在的run静态方法
还传了一个参数
Array ( [0] => d [1] => e [2] => f )

相关推荐:

php的魔术方法__get(),__set(),__call(),__callStatic()以及static用法详解

php中 __call 与 __callStatic用法与区别详解

php __call and __callStatic

以上就是PHP__call与__callStatic使用方法详解的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。