1

In PHP, all object-variables are actually pointers to objects (no?), the language handles this implicitly (right?), yet I see many php code specifying references in parameters such as this:

function someMethod(SomeClass& $obj)
{
//...
}

I've also seen things like this:

function add()
{
 $object = new SomeClass;
 self::$objects[] =& $object;
}

Correct me if I'm wrong, but there wouldn't be any difference here:

self::$objects[] =& new SomeClass
self::$objects[] = new SomeClass

Am I right??????

Another thing I tested:

class SomeClass{}
$obj =& new SomeClass; // is in fact deprecated, doesn't work

$obj = new SomeClass;
$obj2 =& $obj; // works, but should also be deprecated!! No?
fabio
  • 2,269
  • 5
  • 22
  • 34

3 Answers3

2

In php5, yes, it is redundant and pointless.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • No it's not. `&` creates a variable reference, which is quite different than an object reference. See this answer for a detailed breakdown of the difference: http://stackoverflow.com/questions/3611986/in-php-can-someone-explain-cloning-vs-pointer-reference/3612129#3612129 – ircmaxell Dec 15 '10 at 04:43
  • @ircmaxell: in cases OP gave us as a samples - there is no difference. – zerkms Dec 15 '10 at 04:44
  • Sure there is. The last example `$obj2 =& $obj` is very different from `$obj2 = $obj`. Try comparing the result of executing this as the next line: `$obj2 = 'foo';`. What's `$obj` after that? with `=&` it's `foo` as well. With `=` it's the original object. – ircmaxell Dec 15 '10 at 04:47
  • @ircmaxell: i do know the difference and I do know how passing reference by value works. It is pointless to use the same variable for the different types, it is a weird practice. If you have an object and you need to store an int - create another variable. In this point of view `=&` is pointless for objects. – zerkms Dec 15 '10 at 04:50
1

The only thing related to references that is deprecated as far as I know is call-time pass-by-reference (e.g. somefunction(&$var);

Your code samples likely have the & symbol for PHP 4 compatibility. It doesn't make much of a difference whether you use & or not to work with object references in PHP 5. Granted there is a slight difference (between passing references by value in PHP 5, and using & to pass objects by reference), but in most cases it shouldn't affect your code when run in PHP 5.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
0

This page may be helpful to you: http://www.php.net/manual/en/language.operators.assignment.php

new automatically returns a reference, so you don't use the = & anymore with a newly declared object.

zsalzbank
  • 9,685
  • 1
  • 26
  • 39
  • `&=` != `=&` ;-) `=&` (that is what OP asks about) is actually 2 operators `=` and `&` – zerkms Dec 15 '10 at 04:13
  • you are right, my mistake, but the point is still valid. fixed. – zsalzbank Dec 15 '10 at 04:14
  • 1
    @zerkms: I thought it was two operators too (`$somevar = a reference to $someothervar`), but according to Artefacto it is in fact a single operator. See his comment on [this question](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php). – BoltClock Dec 15 '10 at 04:28
  • @BoltClock: omg! syntactic sugar in implementation ;-) thanks for the reference – zerkms Dec 15 '10 at 04:31
  • New does not return a reference. There's nothing to reference since there's no prior object/variable. `new` "returns" the object (in quotes since it's not really returning). `=& new` was deprecated since it doesn't make sense (you're creating a reference to nothing). – ircmaxell Dec 15 '10 at 04:44
  • @ircmaxell: `new` creates an object in memory and returns the reference to that object by value. – zerkms Dec 15 '10 at 04:55
  • From the link above: 'As of PHP 5, the new operator returns a reference automatically, so assigning the result of new by reference results in an E_DEPRECATED message in PHP 5.3 and later, and an E_STRICT message in earlier versions.' – zsalzbank Dec 15 '10 at 13:28