简体   繁体   English

在OOP PHP中$ this->和parent ::之间有什么不同?

[英]What is different between $this-> and parent:: in OOP PHP?

I code something like this to give you an example 我编写这样的代码给你一个例子

This is using "$this->" 这是使用“$ this->”

<?php
class A{
    public function example(){
        echo "A";
    }
}

class B extends A{
    public function example2(){
        $this->example();
    }
}

$b = new B();

echo $b->example2();
?>

and This is using parent:: 这是使用父::

<?php
class A{
    public function example(){
        echo "A";
    }
}

class B extends A{
    public function example2(){
        parent::example();
    }
}

$b = new B();

echo $b->example2();
?>

What is different between $this-> and parent:: in OOP PHP? 在OOP PHP中$ this->和parent ::之间有什么不同?

The difference is that you can access a function of a base class and not of the currient implementation. 不同之处在于您可以访问基类的功能而不是库存实现。

class A {
    public function example() {
        echo "A";
    }

    public function foo() {
        $this->example();
    }
}

class B extends A {
    public function example() {
        echo "B";
    }

    public function bar() {
        parent::example();
    }
}

And here some tests: 这里有一些测试:

$a=new A();
$a->example(); // echos A
$a->foo();     // echos A

$b=new B();
$b->example(); // echos B
$b->foo();     // echos B
$b->bar();     // echos A

parent::example() calls the parent class method, where $this->example() call the current class method. parent::example()调用父类方法,其中$this->example()调用当前的类方法。

In your example there's no difference, since class B doesn't override example() method. 在您的示例中没有区别,因为B类不会覆盖example()方法。 It is common to write something like this (maybe it will help you to understand better this concept): 写这样的东西是很常见的(也许它会帮助你更好地理解这个概念):

    class A {

       public function example(){
           echo 'A';
       }
    }

    class B extends A {

       public function example(){
           echo 'B';
       }

       public function example2(){
          $this->example();
       }


       public function example3() {
          parent::example();
       }
   }

$b = new B();

$b->example2();//print B

$b->example3();//print A

In simple words 简单来说

$this is an instance reference , so whenever you use $this it starts referencing current class methods and properties. $ this是一个实例引用 ,因此无论何时使用$ this,它都会开始引用当前的类方法和属性。

parent is a parent reference which can be used to access parent class properties and methods. parent父引用 ,可用于访问父类属性和方法。

parent:: will call a method or an attribute of the parent. parent::将调用parent::方法或属性。 However, since this is refering to the class and not any kind of instance, you can only call a static method or attribute. 但是,由于这是引用类而不是任何类型的实例,因此您只能调用静态方法或属性。 $this-> refers to the current instance of the object you call this in. $this->指的是你调用它的对象的当前实例。

You could also want to refer to self:: which refers to the current class (once again, no instance involved here) within an object or a static method. 您还可以在对象或静态方法中引用self:: which引用当前类(再次,此处不涉及实例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM