服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - PHP魔术方法之__call与__callStatic使用方法

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

2021-06-07 17:53北京流浪儿 PHP教程

这篇文章主要介绍了PHP魔术方法之__call与__callStatic方法,需要的朋友可以参考下

核心代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//魔术方法__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 )

延伸 · 阅读

精彩推荐