PHP programming process needs to understand the difference between the this,self,parent _php tutorial

Source: Internet
Author: User
A PHP this,self,parent One of the differences in this article
Object-oriented programming (Oop,object oriented programming) 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, 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 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.
Copy CodeThe code is as follows:
Class name//Create a category 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 "
"; 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), 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.
Copy CodeThe code is as follows:
Class Counter//define a 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
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 parent is a pointer to the parent class, and we typically use the parent to invoke the constructor of the parental class. Examples are as follows:
Copy CodeThe code is as follows:
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 inherits from Animal class
Class Person extends Animal
{
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
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 point in the statement ①: Parent::__construct ("Heiyeluren"), when we use the parent to invoke the parents of the constructor to initialize the parent class, so that the object of the inheriting class is assigned the name of the 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 of the parental class.

http://www.bkjia.com/PHPjc/321057.html www.bkjia.com true http://www.bkjia.com/PHPjc/321057.html techarticle {One of the differences in}php this,self,parent This article object-oriented programming (Oop,object oriented programming) has now 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.