The difference between this self parent that you need to know in PHP programming

  • 2020-03-31 19:45:29
  • OfStack

One of the differences between this,self,parent in PHP
OOP (Object Oriented Programming) has become a basic skill for programmers. It is meaningful to improve PHP programming ability and plan web development architecture to make use of OOP to do advanced PHP programming.
When PHP5 was rewritten, it made a big leap in OOP support, becoming a language with most of the features of object-oriented languages, much more object-oriented than PHP4. What I'm going to focus on here is the difference between this,self,parent. Literally, we mean this, ourselves, and our father. As a preliminary explanation, this is a pointer to the current object (you can view it as a pointer in C), self is a pointer to the current class, and parent is a pointer to the parent class. We use Pointers a lot here for lack of a better language. You can refer to the encyclopedia for the concept of Pointers.
Let's talk about it in combination with actual examples.
 
<?php 
class name //A class named name is created
{ 
private $name; //Define properties, private
//Defines a constructor to initialize an assignment
function __construct( $name ) 
{ 
$this->name = $name; //This pointer statement has been used here
} 
//The destructor
function __destruct(){} 
//Print the username member function
function printname() 
{ 
print( $this->name ); //This pointer statement ii is used again, or echo output can be used
} 
} 
$obj1 = new name( "PBPHome" ); //Instantiate object statement
//Perform print
$obj1->printname(); //Output: PBPHome
echo "<br>"; //Output: press enter
//Instantiate the object a second time
$obj2 = new name( "PHP" ); 
//Perform print
$obj2->printname(); //Output: PHP
?> 

Note: the above class used this pointer in statement and statement, so who is this pointing to? In fact, this is in the instantiation of the time to determine who to point to, such as the first instantiation of the object (statement), then this is to point to $obj1 object, so the execution of statement ii print($this-> < Name) becomes print($obj1t-> Name), then of course "PBPHome" is printed. For the second instance, print($this-> Name) becomes print($obj2-> Name), so "PHP" is printed. So, this is a pointer to the current object instance, not to any other object or class.
{2}. The difference between this,self,parent and self in PHP
In this article, we will explain the use of self
The first thing we want to make clear is that self is pointing to the class itself, which means that self is not pointing to any instantiated object, and self is generally used to point to static variables in the class. If we use a static (usually the keyword static) member of the class, we must also use self to call it. Also note that using self to call a static variable must use :: (field operator symbol), see example.
 
<?php 
class counter //Define a class of counter
{ 
//Define the property, including a static variable $firstCount, and assign the initial value of 0 statement
private static $firstCount = 0; 
private $lastCount; 
//The constructor
function __construct() 
{ 
$this->lastCount = ++self::$firstCount; //Use self to call the static variable statement ii
} 
//Print the value of lastCount
function printLastCount() 
{ 
print( $this->lastCount ); 
} 
} 
//Instantiate object
$obj = new Counter(); 
$obj->printLastCount(); //At this point, the program outputs 1
?> 

Notice two places here: statement and statement. We defined a static variable in the statement $firstCount, so in the statement used self to call this value, then we are calling the class's own definition of the static variable $frestCount. Our static variable has nothing to do with the instance of the object below, it just has to do with the class, so I call the class itself, so we can't use this to refer to it, because self is pointing to the class itself, independent of any object instance. Then this calls the instantiated object, $obj, not to be confused.
So much for self, it's a little bit easier to understand with the example. End of canto ii.
{3} the difference between this,self,parent in PHP
In this article, we will discuss the use of parent.
First, let's make it clear that parent is a pointer to the parent class, and we usually use parent to call the constructor of the parent class. Examples are as follows:
 
<?php 
//Create the base class Animal
class Animal 
{ 
public $name; //Property of the base class with the name $name
//Constructor of the base class, initializing the assignment
public function __construct( $name ) 
{ 
$this->name = $name; 
} 
} 
//Define a derived class Person that inherits from the Animal class
class Person extends Animal 
{ 
public $personSex; //For derived classes, the attributes $personSex gender, $personAge age are newly defined
public $personAge; 
//Constructor for a derived class
function __construct( $personSex, $personAge ) 
{ 
parent::__construct( "PBPHome" ); //Parent calls the parent class's constructor statement with parent
$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 the Person object
$personObject = new Person( "male", "21"); 
//Perform print
$personObject->printPerson(); //Output: PBPHome is male,age is 21
?> 

It also contains the use of this, you own analysis. Let's note this detail: the member properties are all public (public properties and methods, code inside and outside the class is accessible), especially the parent class, in order for the inherited class to access through this. The key point is to use parent to call the parent constructor to initialize the parent class, so that all the objects of the inherited class are given the name PBPHome. We can test that by instantiating another object, $personObject1, the name will still be PBPHome.
Summary: this is a pointer to the object instance, to determine the point of the instantiation; Self is a reference to the class itself, usually to a static variable in the class; Parent is a reference to the parent class, which is typically used to call its constructor.

Related articles: