php---魔术方法(__tostring(),__set_state())
看下面这段代码:
|
1
2
3
4
5
6
7
8
|
class Test{ public $a; public function func(){ echo '我只是一个字符串'; }}$test = new Test();echo $test; |
输出的内容为:Catchable fatal error: Object of class Test could not be converted to string in G:xampphtdocswww estclass.php on line 10
如果我们想打印出一个对象,就需要调用__toString()这个魔术方法了,我们给他加上一个__toString()就不会出错了。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
class Test{ public $a; public function func(){ echo '我只是一个字符串'; } public function __toString(){ return "你在打印一个对象的方法"; }}$test = new Test();echo $test; |
__set_state()
这个方法其实也比较简单,就是var_export()的回调函数。看下面这段代码
|
1
2
3
4
5
6
7
8
|
class Test{ public $a; public function func(){ echo '我只是一个方法'; }}$test = new Test();var_export($test); |
注意a是NULL,没有赋值,下面我写个回调
|
1
2
3
4
5
6
7
8
9
10
11
12
|
class Test{ public $a; static function __set_state($array) {//必须是静态方法,参数是一个数组 $tmp = new Test(); $tmp->a = 'abc';//直接赋值 return $tmp;//必须返回一个对象,可以是其他类的对象 } }$test = new Test();eval('$b = '.var_export($test,true).';');var_dump($b); |
有人问这有什么用?是没多大用,最大的作用可以复制一个对象。只需改下代码而已。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Test{ public $a; static function __set_state($array) {//必须是静态方法,参数是一个数组 $tmp = new Test(); $tmp->a = $array['a'];//直接赋值 return $tmp;//必须返回一个对象,可以是其他类的对象 } }$test = new Test();$test->a = '我是$test';eval('$b = '.var_export($test,true).';');var_dump($b); |
有人又会问了,克隆可以直接clone()方法啊!!那么好,这样的情况请问你如何克隆,我们来看下代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Test{ public $a; static function __set_state($array) {//必须是静态方法,参数是一个数组 $tmp = new Test(); $tmp->a = str_replace('$test','$b',$array['a']);//直接赋值 return $tmp;//必须返回一个对象,可以是其他类的对象 } }$test = new Test();$test->a = '我是$test';eval('$b = '.var_export($test,true).';');var_dump($b); |
有的人问,你为什么不用__clone进行回调呢?这正是我想说的地方,个人认为__clone() 没有 __set_state()强大,因为__clone()没有可以接受的参数,局限了“进化”的范围,我用个例子说明。
|
1
2
3
4
5
6
7
8
9
10
11
|
class Test{ public $a; function __clone(){ $this->a = str_replace('a','克隆a',$this->a);//无法通过参数改变a } }$test = new Test();$test->a = '我是a';$b = clone $test;var_dump($b); |