[TYPO3-core] #53682, PHP 5.5.6, call-time pass-by-reference, PHP 5.6

Markus Klein klein.t3 at mfc-linz.at
Fri Dec 13 10:06:29 CET 2013


> Hi,
> > It is WRONG that passing variables by reference is deprecated. It is of
> course not!!!
> > http://php.net/manual/en/language.references.pass.php
> >
> > What has been deprecated long ago is the & operator used on variables in a
> function call. So called "call-time pass-by-reference". (function foo($x) {}    $a
> = ''; foo(&$a);)
> Yes, true.
> > It is indeed a breaking change in PHP 5.6+ that the support of passing
> variables by reference to a constructor is not possible anymore.
> I checked Philipp's patch and as we are expecting an object to be passed,
> there's no point into using the & operator as an object is already passed by
> reference. So it's a breaking change yes and no since using the & operator is
> deprecated since long (perhaps not stated like that but since it does not bring
> anything into using it, at least it's somehow deprecated for long).

Sorry, this is again exactly the same mistake. It is NOT deprecated to use the & operator in a function definition.
The & has nothing to do whether it is an object or not. It is about changing the variable passed from outside!

Two examples:
function foo1(someclass &$s) {
  $s = new someclass('b');
function foo2(someclass $s) {
  $s = new someclass('b');

$a = new someclass('a'); // $a holds the reference to the object. Some number, eg. 0xFA56
foo1($a); // in the function $s now holds the memory address of $a, eg. 0xA56F; (it is a pointer)
// after the call $a holds the reference to the object created in foo1(), eg. 0x65AF

$a = new someclass('a');  // $a holds the reference to the object. Some number, eg. 0xFA56
foo2($a); // in the function $s now holds the reference to the object. $s is a copy of $a, $s now contains 0xFA56 as well
// after the call $a still holds the reference to the original object. eg. 0xFA56

So again: A pass by reference is used to change the original variable and has nothing to do with the fact that objects are passed by reference.

Kind regards

Markus Klein
TYPO3 CMS Active Contributors Team Member

More information about the TYPO3-team-core mailing list