php 7 是否支持按引用传递?

Is pass by reference supported in php 7?

最近我们将 PHP 5.6 迁移到 PHP 7

现在下面的代码抛出 $this->a =& new test($this->f);

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

有什么想法吗?我可以使用什么交替?

根据 PHP7 不兼容的更改: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

别无选择。您使用的是已弃用的行为,现在它不再有效 PHP。不要通过引用分配。

为了澄清 Marc B 的回答:只需像这样删除 & 符号

$this->a = new test($this->f);

您可以选择这样做:

$test = new test($this->f);
$this->a = $test;

现在 $test 是通过引用传递的,如果您更改 $this->a 的属性,$test 属性也会更改。反之亦然。

PHP 7 默认为 "pass by reference"。 如果您不想通过引用传递对象,您应该这样做:

$a = clone $b;