PHP魔术方法之__call与__callStatic使用方法

编程学习 2021-07-05 08:24www.dzhlxh.cn编程入门
这篇文章主要介绍了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 )

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板