Home php教程 php手册 从PHP的引用BUG谈开去

从PHP的引用BUG谈开去

Jun 06, 2016 pm 08:07 PM
bug php Quote article

大蛇写这篇文章是因为TIPI上关于PHP写时复制(Copy-On-Write)问题被同事发到群里,引起了我的兴趣。下面就把这个问题说出来,大家想想为什么: ?php$foo['love']= 1;$bar= $tipi= $foo;$tipi['love']= '2';echo $foo['love']; // 输出 2 相信很多人会认为这

大蛇写这篇文章是因为TIPI上关于PHP写时复制(Copy-On-Write)问题被同事发到群里,引起了我的兴趣。下面就把这个问题说出来,大家想想为什么:

<?php $foo['love']	= 1;
$bar			= &$foo['love'];
$tipi			= $foo;
$tipi['love']		= '2';
echo $foo['love']; // 输出 2
Copy after login

相信很多人会认为这是一个BUG,为什么$foo['love']的值会被改变?在官方的邮件列表中,这个问题也被讨论烂了,与其说是特性,我更愿意说它是个BUG。因为一切有可能挖坑的动作都应该被规避。

为什么会有这样的差异?我们从这里谈开去。

变量类型:
PHP的变量类型有8种,其中NULL和resource是特殊类型,我们常用的有简单类型:int, float, string, boolean,和复合类型:array, object。
何谓简单类型?何谓复合类型?我们来看一看PHP是怎样实现变量的。

typedef union _zvalue_value {
	long lval;					/* long value */
	double dval;				/* double value */
	struct {
		char *val;
		int len;
	} str;
	HashTable *ht;				/* hash table value */
	zend_object_value obj;
} zvalue_value;
typedef struct _zval_struct zval;
struct _zval_struct {
	zvalue_value value;
	zend_uint refcount__gc;
	zend_uchar type;
	zend_uchar is_ref__gc;
};
Copy after login

如上,定义了联合体zvalue_value和结构体zval。
zval是变量的结构,而zval_value就是zval的值。
在zval中type表示数据的类型,他们是:
#define IS_NULL			0
#define IS_LONG		1
#define IS_DOUBLE		2
#define IS_BOOL		3
#define IS_ARRAY		4
#define IS_OBJECT		5
#define IS_STRING		6
#define IS_RESOURCE	7
Copy after login

3(IS_BOOL)及以下类型可以通过联合体 zvalue_value其中的一项来表述,布尔和整型保存在lval中,浮点和双精度浮点保存在double中,NULL是无需保存的,只要type设为IS_NULL就行了。剩下的则麻烦点,比如字符串型的存在struct str中,分别保存字符串和长度,也就是说我们使用 strlen会直接返回长度而无需重新计算字符串长度。数组保存在哈希表ht中,而对象则保存在 obj中。
复合型的变量,如$array['foo']=888;,$array的类型是 IS_ARRAY,而$array['foo']的类型则是IS_LONG,在$array中实际保存的并非888,而是指向$array['foo']的指针。也就是说array的值实际上是一个指针的集合。

我们再看zval中的refcount__gc和 is_ref__gc。refcount__gc是一个计数器,而is_ref__gc则表示该变量是否为引用。那么
$a = &$b; $c=1;
的情况下,$a和$b的is_ref__gc值均为1;$c的is_ref__gc的值为0。
那么refcount__gc在什么时候用呢?那我们接下来说说变量的回收机制。
变量在unset的时候会被注销,那么他的值占用的内存是否马上释放呢?实际上不是,refcount__gc这个计数器就是做这个用途的。
PHP有个特性叫做写时复制(Copy-On-Write),例如:

$a = 1;
$b = $a;
Copy after login

这个时候PHP并不会为$b申请一块新的内存,而是将$a的refcount__gc这个计数器加1,再将$a赋值给$b。当我们echo $b时,实际上读到的是同$a指向同一个内存地址的值。当我们执行:
$b=2;
的时候,PHP会先检查$b是否为引用(这里不是),然后再将$b与$a共同的refcount__gc减1并判断是否为0(这里不是,而是1),那么PHP会重新为$b申请一块新内存,复制$a的值,再修改为2,这个时候$b的refcount__gc发生一次自增,变为1。
那么PHP在unset($b)时所做的就是判断它的refcount__gc在减1后是否为0,如果是,那么则回收(实际上也并没有释放内存,只是放到缓冲区,等满了再释放);如果不是,则只把$b从符号表删除。

=============== 休息,休息一下 ======== 一休割 ===============
那么我们来分析以下为什么会出现文章开头的那个问题,下面一行行分析:

$foo ['love'] = 1;
// $foo:	refcount=1; isref=0;
// ->love:	refcount=1; isref=0;
$bar  = &$foo['love'];
// $bar:		refcount=2; isref=1;
// $foo->love:	refcount=2; isref=1;
// $foo:		refcount=1; isref=0;
$tipi = $foo;
// $foo:		refcount=2; isref=0;
// $foo->love:	refcount=2; isref=1;
// $tipi:		refcount=2; isref=0;
// $tipi->love:	refcount=2; isref=1;
// 注意,这一步复合类型(array)$foo的refcount自增到2,而$foo['love']还是数组的hashtable指向的另一块内存地址,它并不会被复制
$tipi['love'] = '2';
// 这里的$tipi['love']是一个引用,如同$foo['love']一样
echo $foo['love'];
 // 所以当$tipi['love']改变以后,这里自然会输出 2
Copy after login

理解了吗?
相信你看完这个分析后也会认为这个是PHP的特色,我也是这么想的。知道真相后似乎要推翻之前的结论——这是个BUG。但是仔细想想,这种坑实际上是不应该出现的,所以我还是坚持最开始的想法——这就是个BUG!当然,见仁见智。

引用不要滥用,因为PHP本身已经对变量做了很好的优化。但是有些时候还是该用,比如你实际上想传址而不是传值。

另外,大蛇要提醒一句,在5.4.0中,动态引用已经被取消了,例如:

function myfunc($var){
    $var = 1;
}
myfunc(& $foo)
Copy after login

这里这种写法是会导致错误出现的,正确的用法应该是:
function myfunc(& $var){
    $var = 1;
}
myfunc($foo)
Copy after login

好吧,到这里,就到这里,再见吧!
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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles