Home > Backend Development > PHP Tutorial > Explanation of the differences between this, self and parent in php

Explanation of the differences between this, self and parent in php

WBOY
Release: 2016-07-25 08:59:19
Original
915 people have browsed it
  1. class name //Create a class named name

  2. {
  3. private $name; //Define attributes, private

  4. //Define the constructor for initialization and assignment

  5. function __construct( $name )
  6. {
  7. $this->name = $name; //This pointer statement has been used here①
  8. }

  9. < p> //Destructor
  10. function __destruct(){}

  11. //Print user name member function

  12. function printname()
  13. {
  14. print( $this->name ); / / Use this pointer statement again ②, you can also use echo output
  15. }
  16. }
  17. $obj1 = new name( "PBPHome" ); // Instantiate object statement ③

  18. //Execute printing

  19. $obj1->printname(); //Output: PBPHome
  20. echo "
    "; //Output: Enter

  21. //Second instantiation of the object

  22. $ obj2 = new name( "PHP" );

  23. //Perform printing

  24. $obj2->printname(); //Output: PHP
  25. ?>

Copy code

illustrate: The this pointer is used in statements ① and ②, so who does this point to at that time? In fact, this determines who it points to when instantiating it. For example, when the object is instantiated for the first time (statement ③), then this is pointing to the $obj1 object. Then when executing the print of statement ②, print( $this-> name ), then of course "PBPHome" is output. In the second instance, print( $this->name ) becomes print( $obj2->name ), so "PHP" is output. Therefore, this is a pointer to the current object instance and does not point to any other object or class.

Let’s look at the usage of self.

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. If you use a static member in a class (usually using the keyword static), you must use self to call it. Note that using self to call static variables must use :: (field operator symbol), see example.

  1. class counter //Define a counter class
  2. {
  3. //Define attributes, including a static variable $firstCount, and assign an initial value of 0 Statement ①
  4. private static $firstCount = 0;
  5. private $lastCount;

  6. //Constructor

  7. function __construct()
  8. {
  9. $this->lastCount = ++self::$firstCount; //Use self to call the static variable statement ②
  10. }

  11. //Print the lastCount value

  12. function printLastCount()
  13. {
  14. print( $this->lastCount );
  15. }
  16. }
  17. //Instantiate the object

  18. $obj = new Counter();

  19. $obj->printLastCount(); //When executing here, the program outputs 1

  20. ?>

Copy code

Note: Statement ① and Statement ②. A static variable $firstCount is defined in statement ①, then self is used to call this value in statement ②. At this time, the static variable $frestCount defined by the class itself is called. Static variables have nothing to do with the instances of the following objects. They are only related to the class. If you call the class itself, you cannot use this to reference it, because self points to the class itself and has nothing to do with any object instance. Then, the this used earlier calls the instantiated object $obj.

Finally, we will explain the usage of parent.

Parent is a pointer to the parent class. Generally, parent is used to call the constructor of the parent class.

Example:

  1. //Create the base class Animal
  2. class Animal
  3. {
  4. public $name; //Attributes of the base class, name $name

  5. public function __construct( $name )
  6. {
  7. $this->name = $name;
  8. }
  9. }

  10. //Define derivation Class Person inherits from Animal class

  11. class Person extends Animal
  12. {
  13. public $personSex; //For the derived class, newly defined attributes $personSex gender, $personAge age
  14. public $personAge;

  15. //The constructor of the derived class

  16. function __construct( $personSex, $personAge )
  17. {
  18. parent::__construct( "PBPHome" ); //Use parent to call the constructor statement of the parent class ①
  19. $this->personSex = $personSex;
  20. $this->personAge = $personAge;
  21. }

  22. //Member function of derived class, used for printing, format: name is name, age is age

  23. function printPerson()
  24. {
  25. print( $this->name. " is " .$this->personSex. ",age is " .$this->personAge );
  26. }
  27. }

  28. //Instantiate the Person object

  29. $personObject = new Person( "male", "21");

  30. //Perform printing

  31. $personObject->printPerson(); //Output result: PBPHome is male, age is 21

  32. ?>

Copy the code

which also contains the usage of this. Details: Member properties are all public (public properties and methods, accessible to code inside and outside the class), especially those of the parent class. This is for inherited classes to access through this. The key point is statement ①: parent::__construct( "heiyeluren" ). At this time, parent is used to call the constructor of the parent class to initialize the parent class. In this way, the objects of the inherited class are assigned the name PBPHome. We can test it by instantiating another object $personObject1. After printing, the name is still PBPHome.

Summary: this is a pointer to an object instance, which is determined when instantiated; 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. Generally, parent is used to call the constructor of the parent class.

With the above introduction combining theory and examples, have you already had a deep understanding of these three keywords: this, self, and parent? Programmer's Home, I wish you all the best in your studies and progress.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template