Home  >  Article  >  Backend Development  >  php $this赋值的问题

php $this赋值的问题

WBOY
WBOYOriginal
2016-06-06 20:39:161445browse

<code><?php class A{
    public $a = 1;
    function aa(){
        $b = $this;
        echo $b->a;   //输出1
        $this->a = 2;
        echo $b->a;  //输出2 
    }
}
</code>

想知道第二次输出为么是2????

回复内容:

<code><?php class A{
    public $a = 1;
    function aa(){
        $b = $this;
        echo $b->a;   //输出1
        $this->a = 2;
        echo $b->a;  //输出2 
    }
}
</code>

想知道第二次输出为么是2????

http://php.net/manual/zh/language.oop5.references.php
对象和引用

这个问题的核心在于: 你不了解PHP是怎么传值的。去了解一下吧, 顺便把&符号, 类的__clone魔术方法搞明白。

上面输出2的原因是因为php传递对象默认传递的是引用。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn