Three keywords in PHP this,self and parent Instance usage detailed

Source: Internet
Author: User
first of all, we understand the above three keywords: this,self,parent, from the literal better understanding, refers to this, their father, hehe, more fun, we first set up a few concepts, the three key words are used in what place? 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. We often use pointers here to describe, because there is no better language to express, hehe, the Chinese did not learn. -_-#

That's not quite understood, so let's talk about it in terms of practical examples.
(1) This

The code is as follows:

<?php   class UserName   {        //define attribute           private $name;       Defines the constructor function       construct ($name)       {            $this->name = $name;//The This pointer is already used       }       //destructor       function destruct () {}       //print user name member functions function       printname ()       {            print ($this->name);//Use the This pointer again       }   }   Instantiate object   $nameObject = new UserName ("Heiyeluren");   Perform print   $nameObject->printname ();//output: Heiyeluren   //second instantiation of object   $nameObject = new UserName ("PHP");   Perform print   $nameObject->printname ();//output: PHP   ?>


Let's see, the above class uses the this pointer for each row and line, so who does this point refer to? In fact, this is at the time of instantiation to determine who to point to, such as when the first instantiation of the object (row), then this is pointing to the $nameobject object, then the execution of the printing line when the print ($this-><name) into print ($nameObject->name), then of course the "Heiyeluren" is output. In the second instance, print ($this->name) becomes print ($nameObject->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.
(2) 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.

The code is as follows:

<?php       class Counter       {           //defines a property, including a static variable           private static $firstCount =;           Private $lastCount;           constructor function           construct ()           {                $this->lastcount = ++selft:: $firstCount;//using Self to invoke a static variable, using the self call must use the ::(domain operation symbol)           }           //Print the most numeric           function Printlastcount ()           {                print ($this->lastcount);           }        }   //Instantiate object   $countObject = new Counter ();   $countObject->printlastcount (); Output    ?>

We just need to pay attention to two places, the first and the first line. We define a static variable $firstcount in the second row, and the initial value is, then call this at the time of the row, using the self to invoke, and the middle using "::" To connect, is what we call the domain operator, then we called the class itself defined 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 refer to, we can use the self to refer to, because it is pointing to the class itself, regardless of any object instance. In other words, if the static members of our class, we must also use self to invoke.
(3) Parent
We know that parent is a pointer to the parent class, and generally we use the parent to invoke the constructor of the parental class.

The code is as follows:

<?php//base class Animal {///base class Properties public $name;//name//base class       Constructor public function construct ($name) {$this->name = $name;  }}//derived class class person extends Animal//person class inherits Animal class {public $personSex;//gender public $personAge; Age//Inheritance class constructors function construct ($personSex, $personAge) {parent::construct ("heiyelure n ");            The constructor for the parent class is called using parent $this->personsex = $personSex;       $this->personage = $personAge; } function Printperson () {print ($this->name.        ' is '. $this->personsex. ", this year". $this->personage);   }}//Instantiate person object $personObject = new Person ("male", ""); Perform print $personObject->printperson (); Output: Heiyeluren is male,this year; 

We pay attention to a few details: the member property is public, especially the parent class, for the inheriting class to access through this. We pay attention to the key point, 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.
Summary:
This is a pointer to an object instance, and self is a reference to the class itself, which is a reference to the parent class.

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.