PHP this,self,parent keywords in the difference between the use of detailed

Source: Internet
Author: User
A PHP this,self,parent One of the differences in this article

Object-Oriented programming (Oop,object orientedprogramming) has now become a basic skill for programmers. Using the idea of OOP for high-level programming of PHP is significant for improving PHP programming capabilities and planning the Web development architecture.
PHP5 after rewriting, the support amount of OOP has made a great leap, becomes the language with most object-oriented language features, and has a lot of object-oriented features than PHP4. What I'm talking about here is the difference between the this,self,parent three keywords. In the literal sense, it refers to this, the self, the father. First of all, this is a pointer to the current object (which can be seen as a pointer in C), self is a pointer to the current class, and parent is a pointer to the parent class. We use pointers here frequently to describe them because there is no better language to express them. On the concept of pointers, we can refer to the encyclopedia.
Let's talk about it in terms of actual examples.

The code is as follows:

<?php classname//established a class named name {Private$name;         Defines a property, a private//definition constructor, used to initialize the assignment function construct ($name) {$this->name = $name; This pointer is already used here ①}//destructor function destruct () {}//print user name member function printname () {print ($thi             S->name);   Use the this pointer statement ② again, or you can use the Echo output}} $obj 1 = new name ("Pbphome"); Instantiate object Statement ③//execute print $obj 1->printname ();                                    Output: pbphome echo "<br>"; Output: Enter//The second instantiation of the object $obj 2 = new name ("PHP");                         Perform print $obj 2->printname (); Output: PHP; 

Description: the above class used the this pointer in statement ① and statement ②, then who was this? In fact this is at the time of instantiation to determine who to point to, such as when the first instantiation of the object (statement ③), then this is to point to the $obj1 object, then the execution of the statement ② print ($this-><name) turned to print ($ Obj1t->name), then of course output the "Pbphome". In the second instance, print ($this->name) becomes print ($obj 2->name), so it outputs "PHP". So, this is a pointer to the current object instance and does not point to any other object or class.

Two The difference between this,self,parent in PHP self

In this article, we explain the use of self.
First of all, let's make it clear that self is pointing to the class itself, which means that it does not point to any object that has already been instantiated, and that is generally used to point to static variables in the class. If we use a member of the class that is static (usually the keyword static), we must also use self to invoke it. Also note that using self to invoke a static variable must use:: (domain operator symbol), see instance.

The code is as follows:

<?php    classcounter     //define a counter class    {        //define a property, including a static variable $firstcount, and assign an initial value of 0 statements ①          private static $ Firstcount = 0;        Private $lastCount;        constructor function        construct ()        {             $this->lastcount =++self:: $firstCount;      Use self to invoke a static variable statement ②        }        //print Lastcount numeric        function Printlastcount ()        {             print ($this LastCount);        }    }  Instantiate object  $obj = new Counter (); $obj->printlastcount ();                             When executed here, the program outputs 1?>

Note that there are two local statements ① and statement ②. We define a static variable $firstcount in the statement ①, then when the statement ② uses the self to call this value, then we call the class itself to define the static variable $frestcount. Our static variable is not related to an instance of the following object, it is just about the class, then I invoke the class itself, then we cannot use this to reference, because self is pointing to the class itself, regardless of any object instance. And then the previous use of this called the instantiated object $obj, let's not confuse it.
As far as self is mentioned, the combination of examples is more convenient to understand. End of the second article.

Three PHP this,self,parent The difference between the three parent articles

In this article we will explain the use of the parent.
First, we make it clear that parent is a pointer to the parent class, and we typically use the parent to invoke the constructor of the parental class. Examples are as follows:

The code is as follows:

<?php//Build base class Animal class Animal {public    $name;//properties of base class, name $name    //constructor of base class, initialize assignment public    function Construct ($name)    {         $this->name = $name;    }}//Define derived class person inherits from Animal class person extends Animal {    PU Blic$personsex;       For derived classes, the newly defined attribute $personsex Gender, $personAge age public    $personAge;    Constructors for derived classes function    construct ($personSex, $personAge)    {         parent::construct ("Pbphome");    The constructor statement for the parent class is called using parent ①         $this->personsex = $personSex;         $this->personage = $personAge;    }    A member function of a derived class for printing, format: The name is Name,age is age    function Printperson ()    {         print ($this->name. ' is '. $this->personsex. ", Age is". $this->personage);}     } Instantiate the Person object $personObject = new Person ("male", "21"); Perform print $personObject->printperson ();//output: Pbphome is Male,age are?>

The

also contains the usage of this, and we analyze it ourselves. We pay attention to this detail: the member properties are public (common properties and methods, both internal and external to the Code of the Class), especially the parent class, for the inheriting class to access through this. The key is in the statement ①:parent::construct ("Heiyeluren"), when we use parent to invoke the constructor of the parents to initialize the parent class, so that the object of the inheriting class is assigned the name Pbphome. We can test, then instantiate an object $personobject1, after performing the print name is still pbphome.
Summary: This is a pointer to an object instance that determines the point when instantiated, and self is a reference to the class itself, which is typically used to point to a static variable in a class, a reference to a parent class, and a constructor that uses parent to invoke the parents.

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.