This, self, parent differences and PHP double colons: usage

Source: Internet
Author: User
Tags getcolor
This, self, parent differences and PHP double colon: usage PHP5 is a language with most of the characteristics of object-oriented languages, with many object-oriented features than PHP4, however, there are some concepts that are also quite confusing, so today I am going to talk about it, but it's not good. please forgive me. (To read this article, you need to understand the object-oriented knowledge of PHP5.) first, we can understand three keywords: this, self, parent. literally, this, self, difference between parent and usage of PHP double colons:

PHP5 is a language that has the characteristics of most object-oriented languages. it has many object-oriented features than PHP4, but some concepts are also quite confusing. so let's talk about it today, sorry. (Read this article to learn about PHP5's object-oriented knowledge)
First, let's take a look at three keywords: this, self, and parent, which are literally easy to understand. it means this: yourself, father, haha, it's 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) this
Class UserName
{
??? // Define attributes ???
???? Private $ name;
???? // Define the constructor
??? Function _ construct ($ name)
???? {
????????? $ This-> name = $ name; // this pointer is already used here
???? }
???? // Destructor
???? Function _ destruct (){}
???? // Print the username member function
???? Function printName ()
???? {
????????? Print ($ this-> name); // this pointer is used again
???? }
}
// Instantiate the object
$ NameObject = new UserName ("heiyeluren ");
// Execute print
$ NameObject-> printName (); // output: heiyeluren
// The second object instantiation
$ NameObject2 = new UserName ("PHP5 ");
// Execute print
$ NameObject2-> printName (); // output: PHP5
?>
We can see that the class above uses the this pointer in 11 rows and 20 rows respectively. 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, then print ($ this-> Name), then of course the output is "heiyeluren ". 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) 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.
???? Class Counter
???? {
???????? // Define attributes, including a static variable
???????? Private static $ firstCount = 0;
???????? Private $ lastCount;
??????? // Constructor
???????? Function _ construct ()
???????? {
????????????? $ This-> lastCount = ++ selft: $ firstCount; // Use self to call static variables. to use self to call static variables, you must use: (domain operator number)
???????? }
???????? // Print the maximum value
???????? Function printLastCount ()
???????? {
?????????????? Print ($ this-> lastCount );
???????? }
??? }
// Instantiate the object
$ CountObject = new Counter ();
$ CountObject-> printLastCount (); // output 1
?>

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
We know that parent is a pointer to the parent class. generally, we use parent to call the constructor of the parent class.
// Base class
Class Animal
{
??? // Attributes of the base class
???? Public $ name; // name
??? // Constructor of the base class
??? Public function _ construct ($ name)
???? {
???????? $ This-> name = $ name;
????? }
}
// Derived class
Class Person extends Animal // Person class inherits Animal class
{
??? Public $ personSex; // gender
??? Public $ personAge; // age
???? // Constructor of the inherited class
???? Function _ construct ($ personSex, $ personAge)
??? {
?????????? Parent: :__ construct ("heiyeluren"); // uses parent to call the constructor of the parent class.
????????? $ This-> personSex = $ personSex;
???????? $ This-> personAge = $ personAge;
??? }
????? Function printPerson ()
???? {
?????????? Print ($ this-> name. "is". $ this-> personSex. ", this year". $ this-> personAge );
?????? }
}
// Instantiate the Person object
$ PersonObject = new Person ("male", "21 ");
// Execute print
$ PersonObject-> printPerson (); // output: heiyeluren is male, this year 21
?>

We should pay attention to the following details: the member attributes are public, especially for the parent class, to be accessed by the inheritance class through this. We should pay attention to the key points: row 25th: parent: _ construct ("heiyeluren"). In this case, we will 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, we can directly use this to call the class in the inheritance class.


In general, THIS is the current object instance, static variable, const constant, constructor, and has nothing to do with the object instance. it is only related to the class, so use ::


Program List: use variables to define external access in the class
??? ??? Class Fruit {
??????? Const CONST_VALUE = 'fruit color ';
??? }
????
??? $ Classname = 'fruit ';
??? Echo $ classname: CONST_VALUE; // As of PHP 5.3.0
????
??? Echo Fruit: CONST_VALUE;
??? ?>
Program List: used outside the class definition ::
??? ??? Class Fruit {
??????? Const CONST_VALUE = 'fruit color ';
??? }
????
??? Class Apple extends Fruit
??? {
??????? Public static $ color = 'red ';
????
??????? Public static function doubleColon (){
??????????? Echo parent: CONST_VALUE. "\ n ";
??????????? Echo self: $ color. "\ n ";
??????? }
??? }
????
??? Apple: doubleColon ();
??? ?>

Program running result:
1 ??? Fruit Color Red
Program List: call the parent method
??? ??? Class Fruit
??? {
??????? Protected function showColor (){
??????????? Echo "Fruit: showColor () \ n ";
?????? }
??? }
????
??? Class Apple extends Fruit
??? {
??????? // Override parent's definition
??????? Public function showColor ()
??????? {
?????????? // But still call the parent function
??????????? Parent: showColor ();
??????????? Echo "Apple: showColor () \ n ";
??????? }
??? }
????
??? $ Apple = new Apple ();
?? $ Apple-> showColor ();? Not Static
??? ?>

Program running result:
1 ??? Fruit: showColor ()
2 ??? Apple: showColor ()
Program List: Use the scope qualifier
??? ??????? Class Apple
??????? {
??????????? Public function showColor ()
??????????? {
??????????????? Return $ this-> color;
??????????? }
??????? }
????
??????? Class Banana
??????? {
??????????? Public $ color;
????
??????????? Public function _ construct ()
??????????? {
??????????????? $ This-> color = "Banana is yellow ";
??????????? }
????
??????????? Public function GetColor ()
??????????? {
??????????????? Return Apple: showColor (); // use the scope qualifier.
??????????? }
??????? }
????
??????? $ Banana = new Banana;
??????? Echo $ banana-> GetColor ();
??? ?>

Program running result:
1 ??? Banana is yellow
Program List: call the method of the base class
??? ????
??? Class Fruit
??? {
??????? Static function color ()
?????? {
??????????? Return "color ";
??????? }
????
??????? Static function showColor ()
??????? {
?????????? Echo "show". self: color ();
??????? }
??? }
????
??? Class Apple extends Fruit
??? {
??????? Static function color ()
??????? {
??????????? Return "red ";
??????? }
??? }
????
??? Apple: showColor ();
??? // Output is "show color "!
????
??? ?>

Program running result:
1 ??? Show color

?

?

?

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.