PHP5.5empty + trick variable pitfalls

Source: Internet
Author: User
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.

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.