0

PHP as declared refusing the use of reference variable (E_DEPRECATED in 5.3, E_STRICT in 5.4). Initially, I thought it was the use of & in all the cases, but it seems not.

So I was wondering, what kind of code using reference is still allowed, and what now throws a E_DEPRECATED/STRICT error?

Cyril N.
  • 38,875
  • 36
  • 142
  • 243

2 Answers2

0

So far and from what I learned, here's the result :

function foo(&$somevar) { /* ... */ }    // Allowed : pass-by-reference
function &foo($somevar) { /* ... */ }    // Allowed : returning-by-reference
$somevar = &othervar; *1                // Allowed

foo(&$somevar);                          // Refused : call-time pass-by-reference
$somevar = & new SomeObject();           // Refused
$somevar = & SomeObject::foo(); *2      // Refused
  1. with $othervar being everything else than an object
  2. With SomeObject::foo() returning a new instance of an object.
Community
  • 1
  • 1
Cyril N.
  • 38,875
  • 36
  • 142
  • 243
0
$somevar = & new SomeObject();

why would you do that? What would you expect to happen? I'd say the only answer would be just to check the manual? Check out http://www.php.net/manual/en/language.references.php , and especially the part about what they are not (e.g.: pointers).

The thing you might read there would be:

Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.

So you have pass by reference and retrun by referene (both with their own manual page)

Nanne
  • 64,065
  • 16
  • 119
  • 163
  • I don't want to do that (never said I would ;)), just want to know what is deprecated and what is not :) – Cyril N. Nov 07 '12 at 08:28
  • Well, it's not really deprecated as I don't think it was ever usefull :). The old way of adding the `&` to the function call was deprecated (as it is not needed anymore), so that's the only thing that changed in 5.3 and 5.4 as the manual says. – Nanne Nov 07 '12 at 08:37
  • And what about other types than Object ? Like arrays and scalar vars returned by a function (`$somevar = &AnObject::foo()`). Is it deprecated ? is it discouraged ? – Cyril N. Nov 07 '12 at 12:59
  • This is called "assigning by reference". Still the best thing is to check out the manual. http://www.php.net/manual/en/language.references.whatdo.php says a lot in the notes. – Nanne Nov 07 '12 at 13:43
  • I ask because after reading the doc, I'm still a bit confused. You still pointing me to the doc, and I still come back to you with questions. Again, after reading the doc, I'm not sure if `$somevar = &AnObject::foo()` will throw a E_DEPRECATED/STRICT if `AnObject::foo()` returns an object, or only if I call `$somevar =& new AnObject();` – Cyril N. Nov 07 '12 at 15:28
  • I keep pointing there because it's where it is written. For the new question there is this quote: `Since PHP 5, new returns a reference automatically, so using =& in this context is deprecated and produces an E_DEPRECATED message in PHP 5.3 and later, and an E_STRICT message in earlier versions`. And the other one a clear example of "call-time pass-by-reference" of a static function, which I quoted above. I probably can't help you anymore, so good luck. – Nanne Nov 07 '12 at 18:27