PHP5.5empty + Magic variable pitfalls today when I was testing the code, I encountered such a question? Dump (int) empty ($ response-& gt; raw_body); $ response_body $ response-& gt; raw_body; dump (. (int) empty ($ response_body) PHP 5.5 empty + Magic variable pitfalls
Why do I have such a question during code testing today?
dump((int)empty($response->raw_body));$response_body = $response->raw_body;dump(' ' . (int)empty($response_body));
? The output result of this code is:
Write
1
0
?
Why? it's strange, isn't it? My $ response is a class object, which gets
raw_body
Properties are obtained through magic functions.
public function __get($property){echo 123;if (property_exists($this, $property)) {return $this->$property;}}
?
For testing, I output 123 in the above code and the result is displayed.
Write
1
123
0
?
That is to say
(int)empty($response->raw_body)
The _ get method of the $ response object is not executed.
?
So when using PHP magic variables, you should pay attention to this pitfall.
?