Discuss the differences of this self and parent

  • 2020-06-07 04:07:52
  • OfStack

{1} The differences between this,self and parent in PHP
Object oriented programming (OOP,Object OrientedProgramming) has become a basic skill for programmers. Using OOP's ideas for advanced programming of PHP is of great significance for improving PHP's programming ability and planning web's development architecture.
After PHP5 was rewritten, support for OOP took a big leap forward and became a language with most of the features of object-oriented languages, with many more object-oriented features than PHP4. Here I am mainly talking about the difference between this,self and parent. "Father", "father", "father", "father", "father" this is the pointer to the current object (you can view it as the pointer inside C), self is the pointer to the current class, and parent is the pointer to the parent class. We use Pointers a lot here because there's no better language for it. You can refer to the encyclopedia for Pointers.
Let's talk about it in terms of a practical example.

<?php
  classname          // Set up the 1 called name The class of 
 {
    private$name;         // Define properties, private 
    // Defines a constructor that initializes an assignment 
    function __construct( $name )
    {
         $this->name =$name;         // It's already used here this Pointer statement 
    }
    // The destructor 
    function __destruct(){}
    // Print user name member function 
    function printname()
    {
         print( $this->name);             // Use it again this The pointer statement can also be used echo The output 
    }
 }
 $obj1 = new name("PBPHome");   // Instantiate object   Statement 3. 
 // Perform print 
 $obj1->printname(); // The output :PBPHome
 echo"<br>";                                    // Output: Press enter 
 // The first 2 Subinstantiate the object 
 $obj2 = new name( "PHP" );
 // Perform print 
 $obj2->printname();                         // Output: PHP
 ?>

Note: The above class USES the this pointer in statement and statement respectively. Who is this pointing to? In fact, this is instantiated to determine who to point to, such as the first instance of the object (statement), then this is pointing to $obj1 object, then the statement is printed to print($this-) > < name) becomes print($obj1t- > name), then of course "PBPHome" is printed. The second example is print($this-) > name) becomes print($obj2-) > name), so it printed PHP. So this is a pointer to the current object instance, not to any other object or class.

{2} The differences between this,self and parent in PHP
In this article, we explain the use of self
First of all, let's make it clear that self refers to the class itself, so self does not point to any instantiated object, and self is generally used to point to static variables in the class. If we use static (1 static) members of the class, we must also use self to invoke. Also note that using self to invoke static variables must use :: (domain operation notation), as shown in the example.

<?php
    classcounter     // define 1 a counter The class of 
    {
        // Define attributes, including 1 Static variables $firstCount And assign the initial value 0  Statements (1)   
        private static $firstCount = 0;
        private $lastCount;
        // The constructor 
        function __construct()
        {
             $this->lastCount =++self::$firstCount;      // use self To call a static variable   Statement (2) 
        }
        // print lastCount The numerical 
        function printLastCount()
        {
             print( $this->lastCount );
        }
    }
  // Instantiate object 
  $obj = new Counter();
 $obj->printLastCount();                             // When it gets here, the program outputs 1
 ?>

Notice the two local statements and. We defined a static variable $firstCount in statement, so we call the value using self in statement, then we call the static variable $frestCount defined by the class itself. Our static variable has nothing to do with the instance of the following object, it just has to do with the class, so If I call the class itself, then we can't use this to refer to it, because self refers to the class itself, not to any object instance. Then this, used earlier, calls the instantiated object $obj, not to be confused with.
So much for self, combined with examples, it is more convenient to understand. The end of chapter 2.

{3} The difference between this,self and parent
In this article, we explain the use of parent.
First of all, let's make it clear that parent is a pointer to the parent class, and generally we use parent to call the constructor of the parent class. Examples are as follows:

<?php
 // To establish a base class Animal
 class Animal
 {
    public $name; // Property of the base class, name $name
    // Constructor of the base class that initializes the assignment 
    public function __construct( $name )
    {
         $this->name = $name;
    }
 }
 // Defining derived classes Person  Inherited from Animal class 
 class Person extends Animal
 {
    public$personSex;       // For derived classes, new attributes are defined $personSex Gender, $personAge age 
    public $personAge;
    // The constructor of a derived class 
    function __construct( $personSex, $personAge )
    {
         parent::__construct( "PBPHome");    // use parent The constructor of the parent class is called   Statements (1) 
         $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 );
     }
 }
 // instantiation Person object 
 $personObject = new Person( "male", "21");
 // Perform print 
 $personObject->printPerson();// Output results: PBPHome is male,age is 21
 ?>

It also contains the use of this. Note that the member properties are public (public properties and methods, accessible to code inside and outside of the class), especially the parent class, in order for the inherited class to be accessed through this. The key is with the statement: parent:: ___ ("heiyeluren"), and we use parent to call the constructor with the parent class to initialize it so that the objects of the inherited class are all assigned to name as PBPHome. We can test it by instantiating an object $personObject1 and printing name to PBPHome.
Conclusion: this is a pointer to an object instance, which determines the pointer during instantiation. self is a reference to the class itself, generally used to point to static variables in the class; parent is a reference to the parent class, and parent is generally used to call the constructor of the parent class.

Related articles: