The difference between this,self,parent in PHP

Source: Internet
Author: User
Tags php programming

{One of the differences in}php this,self,parent this article

Object-Oriented programming (Oop,object orientedprogramming) has now become a basic skill for programmers. Using the idea of OOP for high-level programming of PHP is significant for improving PHP programming capabilities and planning the Web development architecture.

PHP5 after rewriting, the support amount of OOP has made a great leap, becomes the language with most object-oriented language features, and has a lot of object-oriented features than PHP4. What I'm talking about here is the difference between the this,self,parent three keywords. In the literal sense, it refers to this, the self, the Father . First of all, thisis a pointer to the current object (which can be seen as a pointer in C), self is a pointer to thecurrent class , and parentis a pointer to the parent class . We use pointers here frequently to describe them because there is no better language to express them. On the concept of pointers, we can refer to the encyclopedia.

Let's talk about it in terms of actual examples.

<?php

ClassName//Set up a class named name
{
Private$name; Define properties, Private

Defining 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
function Printname ()
{
Print ($this->name); Use the this pointer statement ② again, or you can use the Echo output
}
}
$obj 1 = new name ("Pbphome"); Instantiating an object statement ③

Perform printing
$obj 1->printname (); Output: Pbphome
echo "<br>"; Output: Enter

Second instantiation of the object
$obj 2 = new name ("PHP");

Perform printing
$obj 2->printname (); Output: PHP
?>

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.

{two}. The difference between this,self,parent in PHP self

In this article, we explain the use of 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. If we use a member of the class that is static (usually the keyword static), we must also use self to invoke it. Also note that using self to invoke a static variable must use :: (domain operator symbol), see instance.

<?php

    classcounter     // Defines a counter class
    {
        //defines a property, including a static variable $firstcount, and assigns an initial value 0  statement ①  
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PRIVATE&NBSP; static   $firstCount = 0;
        private $lastCount;

        //constructor
        function __construct ()
        {
              $this->lastcount =++ Self:: $firstCount;       //use self to invoke static variables   statements ②
        }

Print Lastcount values
function Printlastcount ()
{
Print ($this->lastcount);
}
}

Instantiating an Object
$obj = new Counter ();

$obj->printlastcount (); When you do this, the program outputs 1.

?>

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.

As far as self is mentioned, the combination of examples is more convenient to understand. End of the second article.

{Three}php the difference of this,self,parent in three parent articles

In this article we will explain the use of the parent.

First, we make it clear that parentis a pointer to the parent class , and we typically use the parent to invoke the constructor of the parental class . Examples are as follows:

<?php
Establish base class animal
Class Animal
{
Public $name; Properties of the base class, name $name

constructor of the base class, initialization assignment
Public function __construct ($name)
{
$this->name = $name;
}
}

 //define derived class person  inherit from Animal class
 class person extends Animal
 {
     public$personsex;       //for derived classes, the newly defined attribute $personsex gender, $personAge age
Span style= "FONT-SIZE:16PX;" >    public $personAge;

    //derived class


           parent::__construct ("Pbphome");      //The constructor of the parent class is called with parent   Statement ①
          $this->personage = $ personage;
&NBSP;&NBSP;&NBSP;&NBSP;}

member functions of derived classes for printing, format: Name is Name,age is age
function Printperson ()
{
Print ($this->name. ' is '. $this->personsex. ", Age is". $this->personage);
}
}

Instantiating a Person object
$personObject = new Person ("male", "21");

Perform printing
$personObject->printperson ();//Output result: Pbphome is Male,age is 21

?>

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.

Summary : This is a pointer to an object instance that determines the point when instantiated, and self is a reference to the class itself, which is typically used to point to a static variable in a class, a reference to a parent class, and a parent to invoke the constructor

Translated from: http://www.cnblogs.com/myjavawork/articles/1793664.html

The difference between this,self,parent in 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.