PHP's assignment by reference is not working as expected

后端 未结 7 1622
迷失自我
迷失自我 2021-01-29 08:04

Why the following

class AClass
{
    public function __construct ()
    {
        $this->prop = \"Hello\";
    }
    public function &get ()
    {
                


        
7条回答
  •  甜味超标
    2021-01-29 08:42

    Your function func() needs to return a value and then it needs to assign to a variable what func() returned. See modified code below:

    function func (&$ref) {
        $ref = &$ref->get();
        return $ref;
    }
    
    $value = new AClass();
    $new_value = func($value);
    var_dump( $new_value );
    

提交回复
热议问题