Difference between $this,self and Parent

$this

1. $this refers to the object.
2. $this point to the current object instance does not point to any other object or class.

::self

1. self:: refer to the class itself, not point to any object.
2. using self:: you can access the static properties and static methods of the current class.

::parent

1. parent:: is refer to the parent class.
2. using parent::, you can access the static properties and static methods of the parent class.
3. invoke parent constructor which is not static.
With the use of self and parent, you allow to avoid explicitly reference the class by name.

php子类是否自动调用父类构造函数

如果子类没自定义构造函数,则自动执行父类的构造函数,

反之,则要显式调用parent::__construct()

 

 

关键字 含义 使用位置: 使用示例
parent: 代表父类(这个类) 肯定在一个方法中 parent::属性或方法;
self: 代表当前其所在的类 肯定在一个方法中 self::静态属性或方法;
$this: 代表调用当前方法的对象 肯定在一个方法中 $this->实例属性或方法;

 


发表评论

电子邮件地址不会被公开。 必填项已用*标注