PHP's assignment by reference is not working as expected

后端 未结 7 1546
迷失自我
迷失自我 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 );
    
    0 讨论(0)
提交回复
热议问题