Home >Backend Development >PHP Tutorial >Understand the usage of this, self, and parent keywords in php5

Understand the usage of this, self, and parent keywords in php5

WBOY
WBOYOriginal
2016-07-25 09:04:311065browse
  1. class UserName

  2. {
  3. //Define properties
  4. private $name;
  5. //Define constructor
  6. function __construct( $name ){
  7. $this->name = $name; //This pointer has been used here
  8. }

  9. //Destructor

  10. function __destruct(){}
  11. //Print user name member function
  12. function printName(){
  13. print( $this->name ); //The this pointer is used again
  14. }
  15. }

  16. //Instantiated object

  17. $nameObject = new UserName( "heiyeluren" );
  18. //Perform printing
  19. $nameObject->printName(); //Output: heiyeluren
  20. //Second instantiation of the object
  21. $nameObject2 = new UserName( "PHP5" );
  22. //Perform printing
  23. $nameObject2-> ;printName(); //Output: PHP5
  24. ?>
Copy code

The above class uses this pointer on lines 11 and 20 respectively, so who is this pointing to at that time? Woolen cloth? In fact, this determines who it points to when instantiating it. For example, when the object is instantiated for the first time (line 25), then this points to the $nameObject object. Then when printing on line 18, print( $this ->name ), then of course "heiyeluren" is output. In the second example, print( $this->name ) becomes print( $nameObject2->name ), so "PHP5" is output. Therefore, this is a pointer to the current object instance and does not point to any other object or class.

(2)self First of all, 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.

  1. class Counter
  2. {
  3. //Define attributes, including a static variable
  4. private static $firstCount = 0;
  5. private $lastCount;

  6. < ;p>//Constructor function
  7. function __construct(){
  8. $this->lastCount = ++selft::$firstCount; //Use self to call static variables. When calling self, you must use:: (field operation symbol)
  9. }

  10. //Print the last count value

  11. function printLastCount(){
  12. print( $this->lastCount );
  13. }
  14. }

  15. / /Instantiate the object

  16. $countObject = new Counter();
  17. $countObject->printLastCount(); //Output 1
  18. ?>

Copy the code

Pay attention to two places: Lines 6 and 12. A static variable $firstCount is defined in the second line, and the initial value is 0. Then this value is called on line 12, using self to call, and using "::" to connect in the middle, which is the so-called domain. operator, then what is called at this time is the static variable $frestCount defined by the class itself. The static variable has nothing to do with the instance of the following object. It is only related to the class. Then when I call the class itself, I cannot use this to reference it. I 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 you want to use static members in the class, you must also use self to call it.

(3)、parent We know that parent is a pointer to the parent class, and parent is generally used to call the constructor of the parent class.

  1. //Base class

  2. class Animal
  3. {
  4. //Attributes of the base class
  5. public $name; //Name
  6. //Constructor of the base class
  7. public function __construct( $name ){
  8. $this->name = $name;
  9. }
  10. }

  11. //Derived class

  12. class Person extends Animal //Person class inherits Animal class
  13. {
  14. public $personSex; //Gender
  15. public $personAge; //Age
  16. //Inherited class constructor
  17. function __construct( $personSex, $personAge ){
  18. parent::__construct( "heiyeluren" ); //Use parent calls the constructor of the parent class
  19. $this->personSex = $personSex;
  20. $this->personAge = $personAge;
  21. }
  22. function printPerson(){
  23. print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );
  24. }
  25. }

  26. //Instantiate the Person object

  27. $personObject = new Person( "male", "21");
  28. //Perform printing
  29. $personObject->printPerson(); //Output: heiyeluren is male, this year 21
  30. ?>

Copy code

Attention to details: Member attributes are all public, especially those of the parent class, for inherited classes to access through this. Note the key points: Line 25: parent:: __construct( "heiyeluren" ), then 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 inherit Use this directly in the class to call.



Statement:
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