Differences between this, self, and parent in PHP

Source: Internet
Author: User

One of the differences between this, self, and parent in {1} PHP

Object-Oriented Programming (OOP) has become a basic skill of programmers. Using OOP for advanced PHP programming makes sense to improve PHP programming capabilities and plan the web development architecture.

After rewriting, PhP5 has made a great leap in support of OOP and become a language with the features of most object-oriented languages, phP4 has many object-oriented features. Here I mainly talk aboutThis, self, parentThe difference between the three keywords. Literally, they referHere, myself and my father. Give a preliminary explanation,This is a pointer to the current object.(Can be seen as a pointer in C ),Self is a pointer to the current class.,Parent is a pointer to the parent class.. Frequently UsedPointerBecause there is no better language to express. For more information about the pointer concept, see the encyclopedia.

Next we will talk about it based on the actual examples.

<? PHP

Classname // creates a class named name
{
Private $ name; // defines attributes, private

// Define the constructor to initialize the assignment.
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); // use the this pointer statement ② again. You can also use echo to output
}
}
$ Obj1 = new name ("pbphome"); // instantiate object statement ③

// Execute print
$ Obj1-> printname (); // output: pbphome
Echo "<br>"; // output: Press ENTER

// The second Object Instantiation
$ Obj2 = new name ("php ");

// Execute print
$ Obj2-> printname (); // output: PHP
?>

Note: The class above uses the this pointer in statement ① and statement ② respectively. Who then will this point? ActuallyThis is used to determine who to point to during instantiation.For example, when the object is instantiated for the first time (statement ③), then this is to point to the $ obj1 object, then print ($ this-> <name) when executing statement ②) if it is changed to print ($ obj1t-> name), then of course "pbphome" is output ". In the second instance, print ($ this-> name) is changed to print ($ obj2-> name), so "php" is output ". Therefore, this is a pointer to the current object instance and does not point to any other object or class.

{2 }. Differences between this, self, and parent in php ii self

In this article, we will explain the usage of self.

First, we need to make it clear that self is pointingClass itselfThat is, self isDoes not point to any instantiated objectGenerally, self is used to point to static variables in the class. Suppose we use the classStatic(Usually use the keyword static), we must also use self to call the member. Also note that the use of self to call static variables must be used::(Domain operator number), see instance.

<? PHP

Classcounter // defines a counter class
{
// Define attributes, including a static variable $ firstcount, and assign the initial value 0 statement ①
PrivateStatic$ Firstcount = 0;
Private $ lastcount;

// Constructor
Function _ construct ()
{
$ This-> lastcount = ++SELF ::$ Firstcount; // use self to call static variable statements ②
}

// Print the value of lastcount
Function printlastcount ()
{
Print ($ this-> lastcount );
}
}

// Instantiate the object
$ OBJ = new counter ();

$ Obj-> printlastcount (); // when it is executed here,ProgramOutput 1

?>

Note the following two statements: 1 and 2. We defined a static variable $ firstcount in statement ①, so we used self to call this value in statement ②. At this time, what we call isStatic variables defined by the class$ Frestcount. 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, because self points to the class itself, it is irrelevant to any object instance. ThenThis calls the instantiated object $ obj.Don't confuse.

As for self, it is easier to understand the examples. The second article ends.

Differences between this, self, and parent in {3} PHP

In this article, we will explain the usage of parent.

First, we make it clear that,Parent is a pointer to the parent class., Generally weUse parent to callConstructor. Example:

<? PHP
// Create a base class animal
Class animal
{
Public $ name; // attribute of the base class, name $ name

// Base class constructor, initialization assignment
Public Function _ construct ($ name)
{
$ This-> name = $ name;
}
}

// Define that the derived class person inherits from the animal class
Class person extends animal
{
Public $ personsex; // For a derived class, the attributes $ personsex gender and $ personage are newly defined.
Public $ personage;

// Constructor of the derived class
Function _ construct ($ personsex, $ personage)
{
Parent: :__ construct ("pbphome ");// Use parent to call the constructor statement of the parent class ①
$ This-> personsex = $ personsex;
$ This-> personage = $ personage;
}

// Member function of the derived class, used for printing. Format: 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 ");

// Execute print
$ Personobject-> printperson (); // output result: pbphome is male, age is 21

?>

It also contains the usage of this, which is analyzed by yourself. We should pay attention to the following details: All Member attributes are public (Public attributes and methods, internal and external class attributesCodeCan be accessed), especially for the parent class,This is for the inheritance class to access through this. The key point is in statement ①: parent ::__ construct ("heiyeluren"). At this time, we use parent to call the constructor of the parent class to initialize the parent class. In this way, all the objects of the inherited class are assigned the name pbphome. We can test and instantiate another object $ personobject1. After printing, the name is still pbphome.

conclusion : This is a pointer to an object instance, which is determined to be pointed to during instantiation. self is a reference to the class itself, it is generally used to point to static variables in the class. parent is a reference to the parent class. It is generally used to call the constructor of the parent 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.