Assigning the return value of new by reference is deprecated

PhpPhp 5.2

Php Problem Overview


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.

It's so confusing..I hope you could help me. Thanks in advance.

Php Solutions


Solution 1 - Php

In PHP5 this idiom is deprecated

$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)

Solution 2 - Php

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:

> Deprecated: Assigning the return value of new by reference is > deprecated in .../php/simplepie.inc on line 738

After reviewing several discussions of this issue, I cleared things up by replacing all the instances of =& new with = new in the simplepie.inc file.

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.

Solution 3 - Php

Perhaps the constructor of MDB2 has some code that uses a $variable =& new ClassName();

Solution 4 - Php

Nitin is correct - the issue is actually in the MDB2 code.

According to https://stackoverflow.com/questions/1530112/replacement-for-pear-mdb2-on-php-5-3/2848407#2848407 you can update to the SVN version of MDB2 for a version which is PHP5.3 compatible.

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.

Solution 5 - Php

& 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.

Solution 6 - Php

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.

$obj= new stdClass();  //Without '&' sign.

Solution 7 - Php

just remove new in the $obj_md =& new MDB2();

Solution 8 - Php

It happened because of PHP 5.3 , which comes in WAMP 2.0i package and not Joomla.

You have two choices to fix it,

either use WAMP 2h (previous version) or download PHP 5.2.9-2 addon from WAMP website.

Solution 9 - Php

Upgrade your pear/MDB2 from console:

# pear upgrade MDB2-beta
# pear upgrade MDB2_Driver_Mysql-beta

Problem solved at version 2.5.0b3

Solution 10 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosé M. GilgadoView Question on Stackoverflow
Solution 1 - PhpPaul DixonView Answer on Stackoverflow
Solution 2 - PhpJohn CrockfordView Answer on Stackoverflow
Solution 3 - PhpNitinView Answer on Stackoverflow
Solution 4 - PhpChris BurgessView Answer on Stackoverflow
Solution 5 - PhpJeshurunView Answer on Stackoverflow
Solution 6 - PhpVishnu NarangView Answer on Stackoverflow
Solution 7 - PhpNever Say DieView Answer on Stackoverflow
Solution 8 - PhpShailesh PatelView Answer on Stackoverflow
Solution 9 - PhpSerhii KovalView Answer on Stackoverflow
Solution 10 - PhpkarthicView Answer on Stackoverflow