Home > Backend Development > PHP Tutorial > PHP 5.5 empty + 戏法变量 的坑

PHP 5.5 empty + 戏法变量 的坑

WBOY
Release: 2016-06-13 11:55:01
Original
926 people have browsed it

PHP 5.5 empty + 魔术变量 的坑

今天在测试代码时遇到这么一个疑问?

			dump((int)empty($response->raw_body));						$response_body = $response->raw_body;						dump(' ' . (int)empty($response_body));
Copy after login

?这段代码的输出结果却是:

写道
1
0

?

为什么会这样呢,很奇怪是不是? 我那个 $response 是一个类对象,其获取

raw_body
Copy after login

属性是通过魔术函数来获取的

public function __get($property)	{		echo 123;		if (property_exists($this, $property)) {			return $this->$property;		}	}
Copy after login

?

为了测试,我在上面的代码中输出了 123,结果显示

写道
1

123

0

?

也就是说

(int)empty($response->raw_body)
Copy after login

时并没有去执行 对象 $response 的 __get 方法

?

所以在用PHP魔术变量的童鞋们 注意下这个坑

?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template