Sự khác nhau function fun1(); function &fun1();

Nếu bạn đặt dấu và trước tên hàm, hàm sẽ trả về tham chiếu cho một biến thay vì giá trị.

https://stackoverflow.com/questions/4355300/difference-between-function-and-function

<?php
    class foo {
        public $value = 42;
    
        public function &getValue() {
            return $this->value;
        }
    }
    
    $obj = new foo;
    $myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
    $obj->value = 2;
    echo $myValue;                // prints the new value of $obj->value, i.e. 2.
?>
<?php
    class foo {
        public $value = 42;
    
        public function &getValue() {
            return $this->value;
        }
    }
    
    $obj = new foo;
    $myValue = $obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
    $obj->value = 2;
    echo $myValue;                // prints the new value of $obj->value, i.e. 2.
    Kết qủa: 42
?>
<?php
    class foo {
        public $value = 42;
        public function getValue() {
            return $this->value;
        }
    }
    $obj = new foo;
    $myValue = $obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
    $obj->value = 2;
    echo $myValue;                // prints the new value of $obj->value, i.e. 2.
    Kết qủa: 42
?>

Last updated