php $this赋值的问题

WBOY
Release: 2016-06-06 20:39:16
Original
1477 people have browsed it

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

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

回复内容:

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

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

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

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

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template