Creating class instance in PHP 5 vs. PHP 4 - use of ampersand (&)

liunx

Guest
Just started using PHP 5 and I'm getting lots of errors that using the ampersand (&) in creating new class instances is deprecated in PHP 5.

Wrong:
$instance = &new Class;

Right:
$instance = new Class;

Naturally I've tried to program correctly and so I've got lots of instances of the "wrong" version all over the place (funny how that works out). Simple enough to globally find/replace, but should I be concerned about running the non-ampersand ("Right" version) code on PHP 4? Most of my clients are still on PHP 4 servers (though about half will be upgrading when Pair Networks moves to PHP 5 - soon I hope). I want to make my code fully compatible with PHP 5, but a lot of it still has to work in PHP 4 for the time being.

I'm guessing it's no big deal. I'm not generating so many objects, or working on sites with so many visitors, that it's likely to slow down any servers. And I'm not aware of what any other pitfalls there may be. Anyone care to reassure me?

- BobIn PHP 4 objects are not passed as a reference by default. This is why you need the ampersand. Otherwise they are passed as a copy.

In PHP 5 objects are passed by reference by default.

let me make an example that will show the difference between 4 & 5


<?php

class test {

$message = "hello";

}

function changeit($object){

$object->message = "bye";

echo $object->message; // outputs bye

}

$first = new test();

echo $first->message; // outputs hello

changeit($first);

echo $first->message; // outputs hello in PHP4 and bye in PHP5


?>


Note; I hope that example works right, I didn't test it and I only just got out of bed :PThe difference between versions of

$thing = new Thing;
// and
$thing =& new Thing;

is very minimal. Both will work fine in version 4 and 5. The latter might save some memory in PHP 4, but as you say causes an issue in version 5.The difference between versions of

$thing = new Thing;
// and
$thing =& new Thing;

is very minimal. Both will work fine in version 4 and 5. The latter might save some memory in PHP 4, but as you say causes an issue in version 5.
Nope. Nope. Nope.

The difference can be extremely significant depending on the code. Going through PHP 4 code and removing ampersands is a very good way to break the code. You need to understand why the & was used in the first place. In some cases the & can be removed. But in many other cases, it cannot be.Nope. Nope. Nope.

The difference can be extremely significant depending on the code. Going through PHP 4 code and removing ampersands is a very good way to break the code. You need to understand why the & was used in the first place. In some cases the & can be removed. But in many other cases, it cannot be.
I am referring specifically to the new keyword, not using references in general.

Yes. Yes. Yes.So basically... the syntax isn't that difference but it can have a huge impact. My example was just one way that this can happen.

To make it compatible with PHP4 and PHP5 you will either need to code without the need for it to be passed by reference, since you cant have the & in php5 and you need it in PHP4. or you will just have to make separate code for eachShrike is right, I'm only asking about new object creation. Apparently in PHP 4, creating new objects without the new keyword actually creates two instances, while in PHP 5 the second is a reference to the first automatically.

Of course I do not want to remove ampersands anywhere else, unless there is a specific need.

From the PHP website:
Since PHP 5, new return reference automatically so using =& in this context is deprecated and produces E_STRICT level message.

Note: Not using the & operator causes a copy of the object to be made. If you use $this in the class it will operate on the current instance of the class. The assignment without & will copy the instance (i.e. the object) and $this will operate on the copy, which is not always what is desired. Usually you want to have a single instance to work with, due to performance and memory consumption issues.

While you can use the @ operator to mute any errors in the constructor when using it as @new, this does not work when using the &new statement. This is a limitation of the Zend Engine and will therefore result in a parser error.

So there I have it - I could have just read the manual. But then we wouldn't have had all this stimulating discussion!


- Bob
 
Back
Top