Assigning the Return Value of New by Reference Is Deprecated

Assigning the return value of new by reference is deprecated

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)

Deprecated: Assigning the return value of new by reference is deprecated on empty lines of code

change

$this->model =& new $model($_db, $_input);

$this->view =& new $view($this->model);

to

$this->model =new $model($_db, $_input);

$this->view =new $view($this->model);

PHP 5.3 and assigning the return value of new by reference

Your feeling is right. It will generally work fine, but there are edge cases where it doesn't.

Using =& has these differences with =:

  • =& 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)

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

Deprecated: Assigning the return value of new by reference is deprecated in phpExcelReader\Excel\reader.php on line 261

May be some old php syntax or method, I changed my code and it worked for me:

Earlier:

$this->_ole =& new OLERead();

Change to: (removed & ampersand)

$this->_ole = new OLERead();

The warning and notices are gone now !

Assigning return value of new by reference is deprecated in PHP 5.3

In PHP 5, objects are handled in a reference-like manner by default. So removing the & probably won't change anything.

But as assigning by reference breaks old references, there might still be a difference.

Deprecated: Assigning the return value of new by reference is deprecated

The errant code isn't in your script, but in /htdocs/modules/googleshopping/class/myTools.php on line 56.

A bit of time in Google suggests that this is a Prestashop module of some sort? Take it up with the module author. Tell them to get their act in gear, new-by-ref has been bad practice since PHP 5.0. Considering that the top Google result is also complaining about deprecated errors (about ereg -- ewwwww), I wish you luck. You might want to find an alternate module...

In the mean time, if you're seeing this on a production site, you'll want to turn off deprecated notices by adjusting error_reporting wherever most appropriate for your code, either in your init / bootstrap file or in php.ini.



Related Topics



Leave a reply



Submit