Heim  >  Artikel  >  Backend-Entwicklung  >  php $this赋值的问题

php $this赋值的问题

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

<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传递对象默认传递的是引用。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn