First of all, let’s understand the above three keywords: this, self, parent, which is easier to understand literally, refers to this, self, father, haha, it’s more fun, let’s establish a few concepts first, Where are these three keywords used? Let’s briefly explain that this is a pointer to the current object (let’s use the pointer in C to look at it), self is a pointer to the current class, and parent is a pointer to the parent. Pointer to class. We frequently use pointers to describe here because there is no better language to express it. Haha, I didn’t learn Chinese well. -_-
# It’s not clear enough to say this, so let’s talk about it based on actual examples.
(1) this
The code is as follows:
<?php class UserName { //定义属性 private $name; //定义 构造函数 function construct( $name ) { $this->name = $name; //这里已经使用了this指针 } // 析构函数 function destruct(){} //打印用户名成员函数 function printName() { print( $this->name ); //又使用了this指针 } } //实例化对象 $nameObject = new UserName( "heiyeluren" ); //执行打印 $nameObject->printName(); //输出: heiyeluren //第二次实例化对象 $nameObject = new UserName( "PHP" ); //执行打印 $nameObject->printName(); //输出:PHP ?>
We see that the above class uses this pointer at line and line respectively, then this is pointing to who? In fact, this determines who it points to when instantiating it. For example, when the object is instantiated for the first time (line), then this points to $nameObject object, then when printing the line, Change print( $this->
(2)self
First of all, we must make it clear that self points to the class itself, that is, self does not point to any instantiated object. Generally, self is used to point to static variables in the class.
The code is as follows:
<?php class Counter { //定义属性,包括一个静态变量 private static $firstCount = ; private $lastCount; //构造函数 function construct() { $this->lastCount = ++selft::$firstCount; //使用self来调用静态变量,使用self调用必须使用::(域 运算符 号) } //打印最次数值 function printLastCount() { print( $this->lastCount ); } } //实例化对象 $countObject = new Counter(); $countObject->printLastCount(); //输出 ?>
We only need to pay attention to two places here, the first and second lines. We defined a static variable $firstCount in the second line, with an initial value of operator, then what we call at this time is the static variable $frestCount defined by the class itself. Our static variable has nothing to do with the instance of the following object, it is only related to the class, then if I call the class itself, then we cannot use this To reference, you can use self to reference, because self points to the class itself and has nothing to do with any object instance. In other words, if there are static members in our class, we must also use self to call them.
(3)parent
We know that parent is a pointer to the parent class. Generally, we use parent to call the constructor of the parent class.
The code is as follows:
<?php //基类 class Animal { //基类的属性 public $name; //名字 //基类的构造函数 public function construct( $name ) { $this->name = $name; } } //派生类 class Person extends Animal //Person类继承了Animal类 { public $personSex; //性别 public $personAge; //年龄 //继承类的构造函数 function construct( $personSex, $personAge ) { parent::construct( "heiyeluren" ); //使用parent调用了父类的构造函数 $this->personSex = $personSex; $this->personAge = $personAge; } function printPerson() { print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge ); } } //实例化Person对象 $personObject = new Person( "male", ""); //执行打印 $personObject->printPerson(); //输出:heiyeluren is male,this year ?>
We pay attention to these few details: member attributes are all public, especially those of the parent class, for inherited classes to access through this. We pay attention to the key point, line: parent::construct( "heiyeluren" ). At this time, we use parent to call the constructor of the parent class to initialize the parent class, because the members of the parent class are all public, so We can directly use this to call in the inherited class.
Summary:
This is a pointer to an object instance, self is a reference to the class itself, and parent is a reference to the parent class.
The above is the detailed content of Detailed explanation of the instance usage of the three keywords this, self and parent in php. For more information, please follow other related articles on the PHP Chinese website!