63

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.

starball
  • 20,030
  • 7
  • 43
  • 238
José M. Gilgado
  • 1,314
  • 3
  • 14
  • 21
  • Are you sure your code isn't: `$obj_md =& new MDB2();` That should indeed issue an warning. – Ionuț G. Stan Jul 06 '09 at 11:38
  • Yes, I am. Actually the Zend studio tells me there is that warning. – José M. Gilgado Jul 06 '09 at 11:52
  • I got the same warning from Zend but it was caused by another libaray I used (xajax). Have a look at the stacktrace and you might find where this error comes from. Anyway just follow [Johns answer](http://stackoverflow.com/a/7732137/956397) to fix it with a simple search and replace. – PiTheNumber Oct 31 '13 at 10:26

10 Answers10

96

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)

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • 5
    No, I don't have that ampersand. :( – José M. Gilgado Jul 07 '09 at 01:10
  • Thanks, the php4 parts of the manual have been rearranged, have corrected the link. – Paul Dixon Nov 19 '09 at 17:06
  • 8
    It also appears that the MDB2 source code itself uses this deprecated assignment method; several people (including myself) get the aforementioned "deprecated" warning message on PHP 5.3 whenever an MDB2 object is created. It looks like the MDB2 team has a bug for it: http://pear.php.net/bugs/bug.php?id=16508 – clint Apr 08 '10 at 14:57
  • I am using tihs in a factory method: `$model=new $model_name();` and having same error, while I haven't yet called that method or class but it is having same error `Deprecated: Assigning the return value of new by reference is deprecated in D:\xampp\php\PEAR\Config.php on line 80` – Hafiz May 02 '12 at 21:01
  • Remove deprecated warning you need to rep[lace =& to =. need to change $obj_md =& new MDB2(); To $obj_md = new MDB2(); – Nikunj K. Sep 27 '13 at 09:00
  • 2
    This answer is great, but it could be further improved by explaining whether or not replacing =& new with =new may have any side effect. In other words, why was this deprecated? Does PHP 5's new no longer return a copy (hence making =& unnecessary), or on the contrary, how does one deal with the cases where assinging by reference actually made a difference? – matteo Apr 28 '15 at 16:58
28

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.

John Crockford
  • 281
  • 3
  • 3
9

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

Nitin
  • 91
  • 1
  • 1
8

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

According to Replacement for PEAR: MDB2 on PHP 5.3 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.

Community
  • 1
  • 1
Chris Burgess
  • 3,551
  • 3
  • 29
  • 42
7

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.
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
Vishnu Narang
  • 605
  • 8
  • 15
7

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

Saqib Omer
  • 5,387
  • 7
  • 50
  • 71
Jeshurun
  • 22,940
  • 6
  • 79
  • 92
  • 25
    "PHP 5 passes all variables by reference by default." This is **not true**. PHP5 passes all **objects** by reference, but other vars default to by value. [Fuller explanation](http://stackoverflow.com/questions/879/php-variables-passed-by-value-or-by-reference) – artfulrobot Mar 13 '12 at 14:19
  • 1
    Exactly what @artfullrobot said. – Jimmy Kane Jan 09 '14 at 13:32
2

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

dur
  • 15,689
  • 25
  • 79
  • 125
0

Upgrade your pear/MDB2 from console:

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

Problem solved at version 2.5.0b3

Serhii Koval
  • 354
  • 2
  • 9
0

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.

-6
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
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
karthic
  • 13
  • 1