当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP几种常见魔术方法与魔术变量解析

PHP几种常见魔术方法与魔术变量解析

2019年03月10日  | 移动技术网IT编程  | 我要评论

原文地址:

先不多说,直接上代码,如下:

 1 class demo
 2 {
 3     private $str = 'str';
 4    
 5     //实例化时自动加载function
 6     public function __construct()
 7     {
 8         echo "start<br/>";
 9     }
10 
11     //__call()用来获取没有定义的function
12     public function __call($name, $arguments)
13     {
14         echo $name.'_call<br>';
15     }
16 
17     //获取私有变量
18     public function __get($name)
19     {
20         // todo: implement __get() method.
21         echo $this->$name.'_get<br/>';
22     }
23 
24     //通过关键字 clone 克隆一个对象时该对象调用__clone()方法
25     public function __clone()
26     {
27         // todo: implement __clone() method.
28     }
29 
30     //__set()设置私有变量的值
31     public function __set($name, $value)
32     {
33         // todo: implement __set() method.
34         $this->$name = $value;
35     }
36 
37     //————callstatic()调用没有被定义的static静态function
38     public static function __callstatic($name, $arguments)
39     {
40         // todo: implement __callstatic() method.
41         echo $name.'_classstatic';
42     }
43 
44     //删除类对象时候自动调用
45     public function __destruct()
46     {
47         // todo: implement __destruct() method.
48         echo "end";
49     }
50 
51 
52 }
53 
54 $class = new demo();
55 $class->success();
56 $class->succ = 111;
57 echo $class->succ;
58 echo $class->str;
59 echo '<br>';
60 $obj = clone $class;
61 print_r($obj);
62 echo '<br>';
63 $class::end();


运行结果:

start
success_call
111str_get

clone
demo object ( [str:demo:private] => str [succ] => 111 ) 
end_classstatic
endend

方法总结:

1. __set(),__get(),__isset(),__unset()可以归之为一类,适用于私有变量的设置、取值、判断、删除的操作。
2. __construct()构造函数,__desctruct()析构函数,实例化类的时候就会产生,有一点不同,构造在最前面,
析构函数在最后面
3. 当调用class中没有定义的方法时,会报错fail error,如果class中定义了__call(),会直接调用__call()方法进行操作。
例如:$class->success('data');类中的__call方法开始执行把参数转换为数组形式array([0] => 'data');
__callstatic()方法同理,只是对没有定义的静态方法起作用。

 

几种常见的魔术变量:

 1 namespace app;
 2 
 3 //__line__ 当前脚本行号
 4 echo __line__.'<br/>';
 5 
 6 //__file__ 文件的完整路径与文件名
 7 echo __file__.'<br/>';
 8 
 9 //__dir__ 文件所在目录
10 echo __dir__.'<br/>';
11 
12 class test {
13     function demo(){
14         //__function__ 函数名称 ,php5以后返回该函数被定义时的名字(区分大小写)
15         echo __function__.'<br/>';
16 
17         //__class__ 类名称,php 5 起本常量返回该类被定义时的名字(区分大小写)。
18         //注意自 php 5.4 起 __class__ 对 trait 也起作用。
19         //当用在 trait 方法中时,__class__ 是调用 trait 方法的类的名字。
20         echo __class__.'<br/>';
21 
22         //__method__ 类的方法名,返回该方法被定义时的名字(区分大小写)
23         echo __method__.'<br/>';
24 
25 
26         //__namespace__ 当前命名空间
27         echo __namespace__.'<br/>';
28     }
29 }
30 
31 (new test())->demo();
32 
33 trait helloworld {
34     public function sayhello() {
35         //__trait__ trait 的名字  php 5.4 起此常量返回 trait 被定义时的名字(区分大小写)
36         echo __trait__.'<br/>';
37     }
38 }
39 
40 class theworldisnotenough {
41     use helloworld;
42 }
43 $o = new theworldisnotenough();
44 $o->sayhello();

输出结果:

12
g:\phpstudy\phptutorial\www\phpdemo\03-08.php
g:\phpstudy\phptutorial\www\phpdemo
demo
app\test
app\test::demo
app
app\helloworld

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网