Introduction to PHP5 pointer categories

Source: Internet
Author: User

Many othersFirst of all, let's understand three keywords: this, self, and parent, which are literally easy to understand. this refers to myself, Father, haha. It's quite fun, let's first establish several concepts. Where are these three keywords used? This is a pointer to the current object (let's look at the pointer in C), self is a pointer to the current class, and parent is a pointer to the parent class.

If we do not know much about this, let's talk about it based on the actual examples.

(1) PHP5 pointer to this

 
  1. <?Php
  2.  
  3. Class UserName
  4. {
  5. // Define attributes
  6. Private $ name;
  7.  
  8. // Define the constructor
  9. Function _ construct ($ name)
  10. {
  11. $ This->Name= $ Name; // this pointer is already used here
  12. }
  13.  
  14. // Destructor
  15. Function _ destruct (){}
  16.  
  17. // Print the username member function
  18. Function printName ()
  19. {
  20. Print ($ this->Name); // this pointer is used again
  21. }
  22. }
  23.  
  24. // Instantiate the object
  25. $NameObject=NewUserName ("heiyeluren ");
  26.  
  27. // Execute print
  28. $ NameObject->PrintName (); // output: heiyeluren
  29.  
  30. // The second Object Instantiation
  31. $NameObject2=NewUserName ("PHP5 ");
  32.  
  33. // Execute print
  34. $ NameObject2->PrintName (); // output: PHP5
  35. ?> 

We can see that the class above uses the this pointer in row 11 and row 20 respectively, so who is this pointing? In fact, this is used to determine who to point to during instantiation. For example, when the object is first instantiated (25 rows), then this is to point to the $ nameObject object, when printing 18 rows, the print ($ this-> <name) is changed to the print ($ nameObject-> name), and the "heiyeluren" is output ". In the second instance, print ($ this-> name) is changed to 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) PHP5 pointer self

First, we need to make it clear that self points to the class itself, that is, self does not point to any instantiated object. Generally, self points to static variables in the class.

 
  1. <?Php 
  2.  
  3. Class Counter
  4. {
  5. // Define attributes, including a static variable
  6. Private static $FirstCount=0;
  7. Private $ lastCount;
  8. // Constructor
  9. Function _ construct ()
  10. {
  11. $ This->LastCount= ++ Selft: $ firstCount; // use self to call static variables. The following must be used to call static variables: (domain operator number)
  12. }
  13. // Print the maximum value
  14. Function printLastCount ()
  15. {
  16. Print ($ this->LastCount );
  17. }
  18. }
  19.  
  20. // Instantiate the object
  21. $CountObject=NewCounter ();
  22. $ CountObject->PrintLastCount (); // output 1
  23. ?> 

Here we only need to pay attention to two places, 6th rows and 12th rows. We defined a static variable $ firstCount in the second row, and the initial value is 0. Therefore, when the value is called in 12 rows, we use self to call the variable, and we use ":" in the middle to connect, which is our so-called domain operator. At this time, we call the static variable $ frestCount defined by the class, our static variables have nothing to do with the instance of the following object. They are only related to the class. So if I call the class itself, we cannot use this for reference. We can use self for reference, because self points to the class itself and has nothing to do with any object instance. In other words, we must also use self to call static members in our class.

(3) parent of PHP5 pointer

We know that parent is a pointer to the parent class. Generally, we use parent to call the constructor of the parent class.

 
  1. <?Php 
  2. // Base class
  3. Class Animal
  4. {
  5. // Attributes of the base class
  6. Public $ name; // name
  7. // Constructor of the base class
  8. Public function _ construct ($ name)
  9. {
  10. $ This->Name= $ Name;
  11. }
  12. }
  13. // Derived class
  14. Class Person extends Animal // Person class inherits Animal class
  15. {
  16. Public $ personSex; // gender
  17. Public $ personAge; // age
  18. // Constructor of the inherited class
  19. Function _ construct ($ personSex, $ personAge)
  20. {
  21. Parent: :__ construct ("heiyeluren"); // uses parent to call the constructor of the parent class.
  22. $ This->PersonSex= $ PersonSex;
  23. $ This->PersonAge= $ PersonAge;
  24. }
  25. Function printPerson ()
  26. {
  27. Print ($ this->Name. "is". $ this->PersonSex. ", this year". $ this->PersonAge );
  28. }
  29. }
  30. // Instantiate the Person object
  31. $PersonObject=NewPerson ("male", "21 ");
  32.  
  33. // Execute print
  34. $ PersonObject->PrintPerson (); // output: heiyeluren is male, this year 21
  35.  
  36. ?> 
We should pay attention to the following details: the member attributes are all public, especially the parent class, to be accessed by the inheritance class through this. Note the key points. Row 25th: parent: _ construct ("heiyeluren "), at this time, we will use the parent in the PHP5 pointer to call the constructor of the parent class to initialize the parent class, because the members of the parent class are all public, therefore, we can directly use this to call the inheritance class.

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.