PHP's assignment by reference is not working as expected

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

Why the following

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


        
7条回答
  •  暖寄归人
    2021-01-29 08:21

    what you want can be acheived by this-

     prop = "Hello";
        }
        public function &get ()
        {
            return $this->prop;
        }
        protected $prop;
    }
    
    function func (&$ref)
    {
       $ref= $ref->get();
    
    }
    
    $value = new AClass();
    func($value);
    print_r( $value );
     ?>
    

提交回复
热议问题