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

PHP 5.5 empty + 戏法变量 的坑

Jun 13, 2016 am 11:55 AM
body empty property raw response

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魔术变量的童鞋们 注意下这个坑

?

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if chkdsk cannot be used by the raw drive? What should I do if chkdsk cannot be used by the raw drive? Mar 06, 2023 pm 03:27 PM

Solution to the problem that chkdsk cannot be used by the raw drive: 1. Right-click the Win icon on the bottom taskbar and click the "Run" option in the pop-up menu bar; 2. Enter "chkdsk /?" in the command line window, and then click the Enter key to confirm ;3. Wait for the chkdsk tool to run successfully.

PHP Notice: Trying to get property of non-object - Solution PHP Notice: Trying to get property of non-object - Solution Aug 17, 2023 am 09:27 AM

PHPNotice: Tryingtogetpropertyofnon-object-Solution During the PHP development process, we may encounter a common error message: Tryingtogetpropertyofnon-object (trying to get the property of a non-object). This error is usually caused when we try to access a property (or call a method) on a variable that is not an object type. This article will introduce you to this

Solution to PHP Notice: Undefined property: Solution to PHP Notice: Undefined property: Jun 22, 2023 pm 02:48 PM

When writing code in PHP, we may encounter the error message "Notice: Undefinedproperty". This error means that we are accessing an undefined property, usually because the property has not been initialized in the code. So, how to solve this problem? Here are a few possible solutions: Initialize properties This is the simplest way to solve this problem. Explicitly initializing a property in code ensures that it is defined before use. For example: class

Laravel Development: How to return a response using Laravel Response? Laravel Development: How to return a response using Laravel Response? Jun 14, 2023 am 10:39 AM

Laravel is a popular PHP web development framework that provides many useful features and components, including response return. Response return is a very important concept in Laravel as it controls how the web application provides information to the client. In this article, we will detail the various ways Laravel responses are returned and how to use LaravelResponse to return responses. To return a string in Laravel, you can use the Response object

PHP determines whether a specified key exists in an array PHP determines whether a specified key exists in an array Mar 21, 2024 pm 09:21 PM

This article will explain in detail how PHP determines whether a specified key exists in an array. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP determines whether a specified key exists in an array: In PHP, there are many ways to determine whether a specified key exists in an array: 1. Use the isset() function: isset($array["key"]) This function returns a Boolean value, true if the specified key exists, false otherwise. 2. Use array_key_exists() function: array_key_exists("key",$arr

TypeError: Cannot read property 'XXX' of null in Vue, what should I do? TypeError: Cannot read property 'XXX' of null in Vue, what should I do? Nov 25, 2023 pm 01:21 PM

Vue is a popular JavaScript framework for building user interfaces. During the development process, we may encounter various errors and exceptions. One of the common errors is "TypeError:Cannotreadproperty'XXX'ofnull". In this article, we will explore the causes of this error and how to fix it. First, let’s understand the reason behind this error. When we try to access a property or method of an object, if the pair

TypeError: Cannot read property 'XXX' of undefined in Vue, what should I do? TypeError: Cannot read property 'XXX' of undefined in Vue, what should I do? Nov 25, 2023 am 10:56 AM

TypeError:Cannotreadproperty'XXX'ofundefined in Vue, what should I do? For front-end developers who use Vue to develop, they may often encounter TypeError:Cannotreadproperty'XXX'ofundefined errors during the development process. This error usually occurs when trying to access an undefined property. exist

TypeError: Cannot read property '$XXX' of undefined in Vue, what are the solutions? TypeError: Cannot read property '$XXX' of undefined in Vue, what are the solutions? Nov 25, 2023 am 10:00 AM

TypeError:Cannotreadproperty'$XXX'ofundefined in Vue, what are the solutions? In Vue development, errors such as TypeError:Cannotreadproperty'$XXX'ofundefined are often encountered. This error is usually caused by using undefined properties or methods in the Vue instance. When this error occurs, we need to

See all articles