Understanding the This,self,parent keyword usage in php5

Source: Internet
Author: User
    1. Class UserName

    2. {
    3. Defining properties
    4. Private $name;
    5. Defining constructors
    6. function __construct ($name) {
    7. $this->name = $name; The this pointer is already used here
    8. }

    9. Destructors

    10. function __destruct () {}
    11. Print User name member function
    12. function Printname () {
    13. Print ($this->name); The this pointer is also used
    14. }
    15. }

    16. Instantiating an Object

    17. $nameObject = new UserName ("Heiyeluren");
    18. Perform printing
    19. $nameObject->printname (); Output: Heiyeluren
    20. Second instantiation of the object
    21. $nameObject 2 = new UserName ("PHP5");
    22. Perform printing
    23. $nameObject 2->printname (); Output: PHP5
    24. ?>
Copy Code

The above class uses the this pointer in 11 rows and 20 rows respectively, so who is this at that point? In fact this is at the time of instantiation to determine who to point to, such as when the first instantiation of the object (25 rows), then this is pointing to the $nameobject object, then the execution of 18 lines of printing when the print ($this , name), Then of course the output is "Heiyeluren". In the second instance, print ($this->name) becomes print ($nameObject 2->name) and the "PHP5" is output. So, this is a pointer to the current object instance and does not point to any other object or class.

(2) Self first, to be clear, is to point to the class itself, that is, that it does not point to any object that has already been instantiated, and that is typically used to point to a static variable in the class.

    1. Class Counter

    2. {
    3. Defining attributes, including a static variable
    4. private static $firstCount = 0;
    5. Private $lastCount;

    6. constructor function

    7. function __construct () {
    8. $this->lastcount = ++selft:: $firstCount; Use self to invoke a static variable, using the self call must use::(domain operator symbol)
    9. }

    10. Print the maximum number of times

    11. function Printlastcount () {
    12. Print ($this->lastcount);
    13. }
    14. }

    15. Instantiating an Object

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

Copy Code

Note Two places: lines 6th and 12th. A static variable $firstcount is defined in the second row, and the initial value is 0, then the value is called at 12 rows, using self to invoke, and the middle uses "::" To connect, that is, the so-called domain operator, then called the class itself defined static variable $ Frestcount, a static variable is not related to an instance of the following object, it's just about the class, and I can't use this to refer to the class itself, which can be referenced by self, because self is pointing to the class itself, regardless of any object instance. In other words, if you want to use a static member inside a class, you must also use self to invoke it.

(3), parent we know that parent is a pointer to the parent class, and the parent is generally used to invoke the constructor of the parental class.

  1. Base class

  2. Class Animal
  3. {
  4. Properties of the base class
  5. Public $name; Name
  6. Constructors for base classes
  7. Public function __construct ($name) {
  8. $this->name = $name;
  9. }
  10. }

  11. Derived classes

  12. Class Person extends Animal//person classes inherit the Animal class
  13. {
  14. Public $personSex; Gender
  15. Public $personAge; Age
  16. Constructors for inheriting classes
  17. function __construct ($personSex, $personAge) {
  18. Parent::__construct ("Heiyeluren"); The constructor for the parent class was called with parent
  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. Instantiating a Person object

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

Copy Code

Note The details: The member properties are public, especially the parent class, for the inheriting class to access through this. Note the key: the 25th line: Parent: __construct ("Heiyeluren"), when we use the parent to invoke the constructor of the parents to initialize the parent class, because the members of the parent class are public. We are then able to invoke this directly in the inheriting class using this.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.