PHP---Magic Method (__tostring (), __set_state ())
Look at the following code:
?
12345678 |
class Test{
public $a
;
public function func(){
echo ‘我只是一个字符串‘
;
}
}
$test =
new Test();
echo $test
;
|
The output is:catchable fatal error: Object of Class Test could not being converted to string in G:\XAMPP\HTDOCS\WW w\testclass.php on line ten
If we want to print out an object, we need to call __tostring () This magic method, we add to him a __tostring () will not be wrong.
?
123456789101112 |
class Test{
public $a
;
public function func(){
echo ‘我只是一个字符串‘
;
}
public function __toString(){
return "你在打印一个对象的方法"
;
}
}
$test =
new Test();
echo $test
;
|
Will output:the way you're printing an object
__set_state ()
This method is also relatively simple, is the Var_export () callback function. Look at the code below.
?
12345678 |
class Test{
public $a
;
public function func(){
echo ‘我只是一个方法‘
;
}
}
$test =
new Test();
var_export(
$test
);
|
output: test::__set_state (Array (' a ' = NULL,))
Note that A is null, there is no assignment, I write a callback below
?
123456789101112 |
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
);
|
the output is: object (Test) #2 (1) {["a"]=> string (3) "abc"}
Someone asks what's the use of this? is not much use, the biggest function can copy an object. Just change the code.
?
12345678910111213 |
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
);
|
Output: object (Test) #2 (1) {["a"]=> string (11) "I Am $test"}
Some people will ask, cloning can directly clone () method Ah!! So good, how do you clone this situation, let's look at the code:
?
12345678910111213 |
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
);
|
In this case, we cloned a $test, but we made the corresponding changes.
Some people ask, why don't you __clone a callback? This is exactly what I want to say, personally, that __clone () is not __set_state () strong, because __clone () has no acceptable parameters and limits the scope of "evolution", as I illustrate with an example.
?
1234567891011 |
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
);
|
The result of the output is: object (Test) #2 (1) {["a"]=> string (13) "I Am Clone a"}
PHP---Magic Method (__tostring (), __set_state ())