引用返回

当想用函数找到引用应该被绑定在哪一个变量上面时可以使用引用返回。

<?php

class Foo
{
    /**
     * Just a test property.
     *
     * @var int
     */
    public int $property = 5;

    /**
     * Return a reference.
     *
     * @param  void
     * @return int
     */
    public function &method(): int
    {
        return $this->property;
    }
}

$foo = new Foo();
$property = &$foo->method();
var_dump($property); // int(5)

$foo->property = 6;
var_dump($property); // int(6)

results matching ""

    No results matching ""