PHP 5.3并通过引用分配new的返回值 [英] PHP 5.3 and assigning the return value of new by reference
问题描述
通过引用分配new的返回值已已弃用 PHP 5.3.因此,
Assigning the return value of new by reference has been deprecated in PHP 5.3. As such,
$obj =& new Foo();
现在会引发E_DEPRECATED
错误.
在将具有大量旧代码的大型应用程序升级到5.3时,这会导致很多不必要的通知.
When upgrading a large application with a lot of legacy code to 5.3, this leads to a lot of unwanted notices.
为解决此问题,我正在考虑使用正则表达式查找和替换=& new
的所有实例.例如,以下代码将找到所有PHP文件,并清除=& new
的所有实例:
As a potential fix to this problem, I am considering using a regular expression to find and replace all instances of =& new
with = new
. For example, the following will find all PHP files, and wipe out all instances of =& new
:
find ./ -name '*.php' | xargs perl -p -i -e 's/=(\s*)&(\s*)?new\b/= new/g'
寻找以下问题的答案:
- 它能正常工作吗?我可能会遇到哪些潜在问题?
- 否则,用
= new
替换=& new
的代码示例将更改PHP 5.3中的行为. - 任何与此相关的流行图书馆示例都会引起问题.
- 对于解决大量
=& new
的问题,您还建议其他什么想法?
- Will it work just fine? What potential issues might I run into?
- If not, examples of code where replacing
=& new
with= new
will change the behavior in PHP 5.3. - Any examples of popular libraries with this would be known to cause a problem.
- What other ideas do you recommend to deal with fixing massive amounts of
=& new
?
我怀疑这会很好,但是在寻找可能会遇到麻烦的极端情况下.是的,我知道我可以更改错误报告设置.但是我不想隐藏这些通知,我想修复它们.
I suspect this will work just fine, but looking for edge cases where I might run into trouble. Yes, I know I could just change the error reporting settings. But I don't want to hide the notices, I want to fix them.
推荐答案
您的感觉是正确的.通常它可以正常工作,但是在某些情况下却不能.
Your feeling is right. It will generally work fine, but there are edge cases where it doesn't.
使用=&
与=
有这些区别:
-
=&
将尝试使右侧产生参考;=
不会-即使右侧能够产生引用,例如按引用返回的函数. -
=&
将打破旧的参考集,并将左侧和右侧都放置在一个新的参考集中,而=
会将同一参考集中的所有元素的值更改为左侧的值在右侧.
=&
will try to make the right side yield a reference;=
won't -- even if the right side is able to yield a reference, like a function which returns by reference.=&
will break the old reference set and put the left and the right side both in a new one, while=
will change the value of all the elements in the same reference set as the left side to the value of the right side.
在这种情况下,第一个差异和第二个差异的一半无关紧要.分配后,将只有一个具有新对象*值的变量,并且单元素引用集没有意义.但是,=&
破坏了先前的参考集的事实很重要:
The first difference and half of the second is irrelevant in this case. After the assignment, there will be only one variable with the value of the new object*, and single-element reference sets make no sense. However, the fact that =&
breaks the previous reference set is significant:
<?php
$g = 4;
$v =& $g;
$v = new stdclass();
var_dump($g); // object(stdClass)#1 (0) { }
$g = 4;
$v =& $g;
$v =& new stdclass();
var_dump($g); // int(4)
*除非构造函数可能泄漏引用,但即使泄漏,即使构造函数内部的$this
指向相同的对象,也可能是不同的变量.因此,我怀疑有人会因此而观察到行为差异.
* Unless maybe the constructor leaks a reference, but even if it leaks, $this
inside the constructor may be a different variable, even if it points to the same object. So I doubt one could observe behavior difference due to this.
这篇关于PHP 5.3并通过引用分配new的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!