Assigning the return value of new by reference is deprecated

I recently upgraded my server and now I’m receiving the following error:

“Assigning the return value of new by reference is deprecated in…”

From what little I’ve so far about this, a lot of people claim that it can be fixed by simply removing the reference operator (&).

Is this true? What’s your experience in this?

PHP 4 introduced basic OOP to PHP, however it had a few key issues which were fixed in PHP 5.

Bear in mind that OOP is a concept which has feature which should be consistent between the many different programming languages which implement it.

In most programming languages, an object can be passed to functions or other objects by reference, not by value. In other words, rather than passing a new object with the same values, the actual object is passed, meaning changes are kept.

Here’s an example of how it is in PHP 5:

class Person{
    public $Name;
}
function Something(Person $Person){
    $Person->Name = 'Jimbob';
}
$A = new Person();
$A->Name = 'James';
Something($A);
echo $A->Name; //Jimbob

Though, in PHP 4, the same code (IIRC) would output ‘James’, as the function recieves only a copy of $A.

class Person{
    var $Name;
}
function Something($Person){
    $Person->Name = 'Jimbob';
}
$A = new Person();
$A->Name = 'James';
Something($A);
echo $A->Name; //James

Because of this, you had to manually pass the object by reference rather than by value:

class Person{
    var $Name;
}
function Something($Person){
    $Person->Name = 'Jimbob';
}
$A = new Person();
$A->Name = 'James';
Something(&$A);
echo $A->Name; //Jimbob

However, if you do that in PHP5 you’re trying to send a reference by reference, which doesn’t really make much sense.

Basically, it’s done automatically now, so remove the & and it’ll work as before

Having you guys explain this begs the question, “Why didn’t they fix this sooner”?

There was nothing to ‘fix’ as this was the agreed solution for PHP4.

They fixed it in 2004… with the introduction of PHP 5 :wink: