Differences between this, self, and parent in PHP

Source: Internet
Author: User
In PHP, what are the differences between this, self, and parent? in PHP, what are the differences between this, self, and parent? One of the differences between this, self, and parent in {1} PHP is this article ?????? Object-oriented programming (OOP) has become a basic skill of programmers. The idea of OOP is used for advanced PHP programming. The difference between improving PHP programming capability and planning PHP's this, self, and parent

Differences between this, self, and parent in 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 aboutThis, self, parent? The 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.

? Classname ?????????? // Creates a class named name.
? {
???? Private $ name ;????????? // Define 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 this pointer statement ② again, or echo output
????}
?}
? $ Obj1 = new name ("PBPHome ");??? // Instantiate the 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 above classes are in? When statements ① and ② use the this pointer, who would 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), 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 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.

?

???? Classcounter ????? // 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 variables? Statement ②
????????}

???????? // 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, 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:

? // 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 the derived class Person? Inherited from Animal class
? Class Person extends Animal
? {
???? Public $ personSex ;??????? // For the derived class, the new attributes $ personSex gender and $ personAge age are defined.
???? Public $ personAge;

???? // Constructor of the derived class
???? Function _ construct ($ personSex, $ personAge)
???? {
?????????Parent: :__ construct ("PBPHome ");????// Used parent to call the constructor of the parent class? Statement ①
????????? $ 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. it is determined to point to it during instantiation. self is a reference to the class itself and 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.