php 不推荐通过引用分配 new 的返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1086539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning the return value of new by reference is deprecated
提问by José M. Gilgado
I've just got an error.
我刚刚有一个错误。
When I try to assign an object like this:
当我尝试分配这样的对象时:
$obj_md = new MDB2();
The error I get is "Assigning the return value of new by reference is deprecated". Actually I've been looking for a solution but the only one I've seen is just turn down the politicy of php.ini (error_reporting). I've tried it too, but it didn't work.
我得到的错误是“不推荐通过引用分配 new 的返回值”。实际上,我一直在寻找解决方案,但我看到的唯一解决方案就是拒绝 php.ini (error_reporting) 的策略。我也试过了,但是没有用。
It's so confusing..I hope you could help me. Thanks in advance.
太混乱了。。希望你能帮帮我。提前致谢。
回答by Paul Dixon
In PHP5 this idiom is deprecated
在 PHP5 中,这个习语已被弃用
$obj_md =& new MDB2();
You sure you've not missed an ampersand in your sample code? That would generate the warning you state, but it is not required and can be removed.
您确定您没有在示例代码中遗漏一个 & 号吗?这会生成您声明的警告,但它不是必需的,可以删除。
To see why this idiom was used in PHP4, see this manual page(note that PHP4 is long dead and this link is to an archived version of the relevant page)
要了解为什么在 PHP4 中使用此习语,请参阅此手册页(请注意,PHP4 已死很久了,此链接指向相关页面的存档版本)
回答by John Crockford
I recently moved a site using SimplePie (http://simplepie.org/) from a server that was using PHP 5.2.17 to one that is using PHP 5.3.2. It was after this move that I began receiving a list of error messages such as this one:
我最近将一个使用 SimplePie ( http://simplepie.org/)的站点从使用 PHP 5.2.17 的服务器迁移到使用 PHP 5.3.2 的服务器。正是在这一举动之后,我开始收到一系列错误消息,例如:
Deprecated: Assigning the return value of new by reference is deprecated in .../php/simplepie.inc on line 738
已弃用:在 .../php/simplepie.inc 中第 738 行不推荐通过引用分配 new 的返回值
After reviewing several discussions of this issue, I cleared things up by replacing all the instances of =& newwith = newin the simplepie.inc file.
在查看了对这个问题的几次讨论之后,我通过将simplepie.inc 文件中的=& new 的所有实例替换为= new来解决问题。
I'm not experienced enough to know if this will work in all instances where these error messages are received but it worked in this particular case and it may be worth trying.
我没有足够的经验来知道这是否在收到这些错误消息的所有情况下都有效,但它在这种特殊情况下有效,可能值得一试。
回答by Nitin
Perhaps the constructor of MDB2 has some code that uses a $variable =& new ClassName();
也许 MDB2 的构造函数有一些使用 $variable =& new ClassName(); 的代码。
回答by Chris Burgess
Nitin is correct - the issue is actually in the MDB2 code.
Nitin 是正确的 - 问题实际上出在 MDB2 代码中。
According to Replacement for PEAR: MDB2 on PHP 5.3you can update to the SVN version of MDB2 for a version which is PHP5.3 compatible.
根据Replacement for PEAR: MDB2 on PHP 5.3,您可以将 MDB2 的 SVN 版本更新为与 PHP5.3 兼容的版本。
As that answer was given in March 2010, and http://pear.php.net/package/MDB2/shows a release some months later, I expect the current version of MDB2 will solve the issue also.
由于该答案是在 2010 年 3 月给出的,并且http://pear.php.net/package/MDB2/显示了几个月后的一个版本,我希望 MDB2 的当前版本也能解决这个问题。
回答by Jeshurun
& is used in PHP to pass an object to a method / assign a new object to a variable by reference. It is deprecated in PHP 5 because PHP 5 passes all objects by reference by default.
& 在 PHP 中用于将对象传递给方法/通过引用将新对象分配给变量。它在 PHP 5 中被弃用,因为 PHP 5 默认通过引用传递所有对象。
回答by Vishnu Narang
I had the same problem. I already had the '&' and still it was giving the same warning. I'm using PHP 5.3 with WAMP and all i did was REMOVE '&' sign and the warning was gone.
我有同样的问题。我已经有了“&”,但它仍然发出相同的警告。我在 WAMP 中使用 PHP 5.3,我所做的只是删除“&”符号,警告消失了。
$obj= new stdClass(); //Without '&' sign.
回答by Never Say Die
just remove newin the $obj_md =& new MDB2();
只是删除new在$obj_md =& new MDB2();
回答by Serhii Koval
Upgrade your pear/MDB2 from console:
从控制台升级您的梨/MDB2:
# pear upgrade MDB2-beta
# pear upgrade MDB2_Driver_Mysql-beta
回答by Shailesh Patel
It happened because of PHP 5.3 , which comes in WAMP 2.0i package and not Joomla.
这是因为 PHP 5.3 ,它来自 WAMP 2.0i 包而不是 Joomla。
You have two choices to fix it,
你有两个选择来修复它,
either use WAMP 2h (previous version) or download PHP 5.2.9-2addon from WAMP website.
使用 WAMP 2h(以前的版本)或从 WAMP 网站下载 PHP 5.2.9-2插件。
回答by karthic
C:\wamp\www\..\libraries\pattemplate
1.ini_set('display_errors', 0);
$this->_modules[$moduleType][$sig] =&new $moduleClass; wrong
$this->_modules[$moduleType][$sig] =new $moduleClass; Right

