In PHP: the difference between this, self and parent
Here I mainly talk about the difference between the three keywords ** this,self,parent **. Literally, it refers to this, myself and father respectively. To begin with, this is a pointer to the current object (which can be seen as a pointer in C), self is a pointer to the current class, and parent is a pointer to the parent class.
this: Pointer to the current object
<?php classname //Create a class named name { private$name; //Define attributes, private //Define constructors to initialize assignments function __construct( $name ) { $this->name =$name; //this pointer statement has been used here. } //Destructor function __destruct(){} //Print User Name Membership Functions function printname() { print( $this->name); //Use this pointer statement again, or echo output } } $obj1 = new name("PBPHome"); //Instantiate Object Statement (3) //Execution printing $obj1->printname(); //Output: PBPHome echo"<br>"; //Output: Return //Second Instance Object $obj2 = new name( "PHP" ); //Execution printing $obj2->printname(); //Output: PHP ?>
Explanation: The above classes use this pointer in statement 1 and statement 2 respectively, so who is this pointer to at that time?
Actually ** this is when you instantiate to determine who ** is pointing to, such as when you first instantiate an object (statement 3).
Then this was pointing to the $obj1 object.
Then print ($this - > < name) becomes print ($obj1t - > name) when executing statement 2.
Then, of course, "PBPHome" is exported.
In the second instance, print ($this - > name) becomes print ($obj2 - > name), and "PHP" is output.
So this is a pointer to the current object instance, not to any other object or class.
self: refers to the directed class itself
First of all, let's make it clear that self refers to the class itself, that is, self does not refer to any object that has been instantiated. Generally, self is used to refer to static variables in the class.
If we use static members of a class, we must also use self to call.
Also note that using self to invoke static variables must use **:** (field operation symbols), as shown in the example.
<?php classcounter //Define a counter class { //Define attributes, including a static variable, $firstCount, and assign an initial value of 0 statement private static $firstCount = 0; private $lastCount; //Constructor function __construct() { $this->lastCount =++self::$firstCount; //Use self to call static variable statements } //Print lastCount values function printLastCount() { print( $this->lastCount ); } } //Instance object $obj = new Counter(); $obj->printLastCount(); //When it is executed here, the program outputs 1 ?>
Here we should pay attention to two local sentences (1) and (2) sentences.
We define a static variable $firstCount in statement 1, then use self to call this value in statement 2, then we call the static variable $frestCount defined by the class itself.
Our static variables have nothing to do with the instance of the following object. They are only related to the class. If I call the class itself, then we can't use this to refer to it, because self refers to the class itself and has nothing to do with any object instance.
And then the first thing called with this is the instantiated object, $obj, so don't get confused.
Parent: A pointer to the parent class
First, we make it clear that parent is a pointer to the parent class. Generally, we ** use parent to call the constructor of the parent class. Examples are as follows:
<?php //Establishing Base Class Animal class Animal { public $name; //Attribute of base class, name $name //Constructor of base class, initialization assignment public function __construct( $name ) { $this->name = $name; } } //Define the derived class Person to inherit from the Animal class class Person extends Animal { public$personSex; //For derived classes, the attributes $personSex gender, $personAge age are newly defined public $personAge; //Constructors of Derived Classes function __construct( $personSex, $personAge ) { parent::__construct( "PBPHome"); //Use parent to call the constructor statement of the parent class $this->personSex = $personSex; $this->personAge = $personAge; } //Derived class member function for printing, format: name is name,age is age function printPerson() { print( $this->name. " is ".$this->personSex. ",age is ".$this->personAge ); } } //Instantiate Person objects $personObject = new Person( "male", "21"); //Execution printing $personObject->printPerson();//Output: PBPHome is male,age is 21 ?>
It also contains the use of this, we analyze it ourselves.
Let's take note of the details: member attributes are public (public attributes and methods, accessible to both internal and external code of the class), especially the parent class, for inheritance classes to access through this.
The key point is statement 1: parent:: construct ("heiyeluren"), at which time we use parent to call the constructor of the parent class to initialize the parent class, so that the inherited class objects are assigned a name of PBPHome.
We can test that by instantiating an object, $personObject1, the name is still PBPHome after printing.
summary
this is a pointer to an object instance, which is determined when it is instantiated.
self is a reference to the class itself and is generally used to point to static variables in the class.
Parent is a reference to the parent class. Parent is usually used to call the constructor of the parent class.