Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is pass by reference supported in php 7?

Recently we migrated PHP 5.6 to PHP 7

and now following code throws $this->a =& new test($this->f);

Parse error: syntax error, unexpected 'new' (T_NEW) 

any ideas? what are the alternation that I could use for it?

like image 878
FR STAR Avatar asked Dec 04 '22 23:12

FR STAR


2 Answers

To clarify Marc B's answer: just remove the ampersand like this

$this->a = new test($this->f);
like image 39
Bennett McElwee Avatar answered Dec 28 '22 05:12

Bennett McElwee


As per the PHP7 incompatible changes: http://php.net/manual/en/migration70.incompatible.php

New objects cannot be assigned by reference

The result of the new statement can no longer be assigned to a variable by reference: <?php class C {} $c =& new C; ?>

Output of the above example in PHP 5:

Deprecated: Assigning the return value of new by reference is deprecated in /tmp/test.php on line 3

Output of the above example in PHP 7:

Parse error: syntax error, unexpected 'new' (T_NEW) in /tmp/test.php on line 3

There is no alternative. You were using deprecated behavior, and now it's no longer valid PHP. Simply don't assign by reference.

like image 107
Marc B Avatar answered Dec 28 '22 05:12

Marc B