<p>$this,self,parent三个关键字从字面上比较好理解,分别是指这、自己、父亲。</p><p><span style="color: rgb(255, 0, 0);">$this  是指向当前对象的指针;</span></p><p><span style="color: rgb(255, 0, 0);">self    是指向当前类的指针;</span></p><p><span style="color: rgb(255, 0, 0);">parent是指向父类的指针。</span></p><p>$this表示当前所在类对象这个很不明确,$this取决于自己所在方法被调用时的执行环境,当前方法在哪个对象环境下执行,该方法的$this就表示该对象,针对还原一层层到最初始的方法的对象,说得更通俗一点,<strong>$this是指向当前对象的指针</strong>。</p><p><span style="font-size: 18px;"><strong>$this实例</strong></span></p><pre class="brush:php;toolbar:false"><?php class A {     function __construct()     {         $this->test();//这里使用了$this     }     public function test ()     {         echo "https://panxu.net";     } } new A();</pre><p>说明:上面的案例会输出"https://panxu.net",在上面的类中,$this表示当前类,然后调用当前类的test()方法。</p><p>self是指向类本身,也就是self是不指向任何已经实例化的对象,<strong>一般</strong><strong>self使用来指向类中的静态变量</strong>。假如我们使用类里面静态(一般用关键字static)的成员,我们也必须使用self来调用。还要注意使用self来调用静态变量必须使用:: (域运算符号),见实例。</p><p><strong><span style="font-size: 18px;">self实例</span></strong></p><pre class="brush:php;toolbar:false"><?php class B {     public static $url = "https://panxu.net";//定义静态变量     public static function test ()     {         echo self::$url;     } } B::test();</pre><p>说明:上面的案例还是会输出"https://panxu.net",在上面的类中,两次使用到self了,第一次是在test()方法中输出静态变量$url,<strong>表示当前类的属性</strong>;第二次是在类的外部通过类名直接调用当前类的test()方法,<strong>表示当前类的方法</strong>。关于self就说到这里,结合例子还是比较方便理解的。</p><p>最后,parent是指向父类的指针,一般我们使用parent来调用父类的构造函数。实例如下:</p><p><span style="font-size: 18px;"><strong>parent实例</strong></span></p><pre class="brush:php;toolbar:false">class C {     public $url = "https://panxu.net";     public function test ()     {         echo $this->url;     } } class D extends C {     public function __construct ()     {         echo parent::test();     } } new D();</pre><p>说明:上面的案例同样还是会输出"https://panxu.net",这里是使用了两个类进行实现,D类继承C类,然后D类通过parent调用父类的方法,这里的<strong>parent表示父类</strong>。</p><p>总结:this是指向对象实例的一个指针,在实例化的时候来确定指向;self是对类本身的一个引用,一般用来指向类中的静态变量;parent是对父类的引用,一般使用parent来调用父类方法和属性。</p>