The difference between "PHP" this,self,parent

Source: Internet
Author: User

I. Definitions & Differences

Self: A pointer to the current class, selfis not pointing to any object that has been instantiated , and general self uses it to point to a static variable in the class.

This: A pointer to the current object, using parent to invoke the constructor of the parent class.

Parent: Pointer to the parents class

Two. Usage differences

1.self

<?PHP classcounter//define a class of counter    {        //defines a property, including a static variable $firstcount, and assigns an initial value of 0 statements ①        Private Static $firstCount= 0; Private $lastCount; //constructor Function        function__construct () {$this->lastcount =++self::$firstCount;//use self to invoke a static variable statement ②        }        //Print Lastcount Values        functionPrintlastcount () {Print($this-lastCount); }    } //instantiating an object  $obj=NewCounter ();$obj->printlastcount ();//When you do this, the program outputs 1 .?>
View Code

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.

2.this

<?PHP classname//A class named name was created {    Private$name;//define attributes, private//definition constructors, for initializing assignments    function__construct ($name )    {         $this->name =$name;//The This pointer statement has been used here ①    }    // Destructors    function__destruct () {}//Print User name member function    functionPrintname () {Print($this->name);//Use the This pointer statement ② again, or you can use the Echo output    } } $obj 1=NewName ("Pbphome");//instantiating object Statements ③//performing printing $obj 1->printname ();//Output: Pbphome Echo"<br>";//output: Enter//Instantiate object for second time $obj 2=NewName ("PHP" ); //Perform printing $obj 2->printname ();//Output: PHP?>
View Code

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 pointing to the $obj1 object, then the execution of the statement ② print ($this-><name) into the Print ($obj 1t->name), then of course the "pbphome" is output. 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.

3.parent

<?PHP//Establish base class animal classAnimal { Public $name;//The properties of the base class, the constructor of the name $name//base class, initialization assignment     Public function__construct ($name )    {         $this->name =$name; } } //define derived class person inherits from Animal class classPersonextendsAnimal { Public$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");//A constructor statement for the parent class was called using parent ①         $this->personsex =$personSex; $this->personage =$personAge; }    //member functions of derived classes for printing, format: Name is Name,age is age    functionPrintperson () {Print($this->name. "Is".$this->personsex. ", Age is".$this-personage); } } //instantiating a person object $personObject=NewPerson ("Male", "21"); //Perform printing $personObject->printperson ();//output Result: Pbphome is male,age?>
View Code

It 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.

Three. Summary

This is a pointer to an object instance, which is a reference to the class itself, which is typically used to point to a static variable in the class, a reference to the parent class, and a parent to invoke the constructor of the parental class.

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.

The difference between "PHP" this,self,parent (RPM)

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.