Differences between this, self, and parent during PHP programming _ PHP Tutorial

Source: Internet
Author: User
The difference between this, self, and parent needs to be understood during PHP programming. One of the differences between this, self and parent in {1} PHP this article object-oriented programming (OOP, ObjectOrientedProgramming) has become a basic skill for programmers. Using the idea of OOP for PHP 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 about the differences between the three keywords "this", "self", and "parent. Literally, it means this, yourself, and your father. 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 often use pointers to describe them here because there is no better language to express them. For more information about the pointer concept, see the encyclopedia.
Next we will talk about it based on the actual examples.

The code is as follows:


Class name // 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"
"; // Output: 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? In fact, this is used to determine who to point to During instantiation. for example, when an object is instantiated for the first time (statement ③), then this is to point to the $ obj1 object, then print ($ this-> Name), then of course it outputs "PBPHome ". 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 points to the class itself, that is, self does not point to any instantiated object. Generally, self points to static variables in the class. If we use a static member in the class (generally the keyword static), we must also use self to call it. Note that the following must be used to call static variables using self: (domain operator number). For details, see instance.

The code is as follows:


Class counter // define a counter class
{
// Define attributes, including a static variable $ firstCount, and assign the initial value 0 statement ①
Private static $ 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, the program outputs 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, we called 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, because self points to the class itself, it is irrelevant to any object instance. Then, the previous use of this calls the instantiated object $ obj. do not confuse it.
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, we use parent to call the constructor of the parent class. Example:

The code is as follows:


// 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, and code inside and outside the class can be accessed), especially for parent classes, 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.
Summary: this is a pointer to an object instance, which is determined to point to During instantiation; self is a reference to the class itself, which 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.

Object-oriented Programming (OOP) has become a basic skill for programmers. Using the idea of OOP for PHP...

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.