A PHP this,self,parent One of the differences in this article
Object-oriented programming (Oop,object oriented programming) is now a basic skill for programmers. Using OOP for advanced programming in PHP is very useful for improving PHP programming ability and planning web development architecture.
PHP5 after rewriting, the amount of support for OOP has a great leap, has become a majority of object-oriented language features of the language, than the PHP4 has a lot of object-oriented characteristics. Here I mainly talk about the difference between the this,self,parent three keywords. The literal understanding, respectively refers to this, oneself, the father. To begin with, this is a pointer to the current object (which can be viewed as a pointer in C), and self is a pointer to the current class, and parent is a pointer to the parent class. We use pointers frequently to describe it because there is no better language to express it. On the concept of pointers, we can go to the reference encyclopedia.
Let's talk about it in terms of actual examples.
Copy Code code as follows:
<?php
Class name//Creates a classes named name
{
Private $name; Defining properties, Private
Defines a constructor for initializing an assignment
function __construct ($name)
{
$this->name = $name; The this pointer statement is already used here ①
}
destructor
function __destruct () {}
Print User name member functions
function Printname ()
{
Print ($this->name); Again using the this pointer statement ②, or you can use the Echo output
}
}
$obj 1 = new name ("Pbphome"); Instantiating an object statement ③
Performing printing
$obj 1->printname (); Output: Pbphome
echo "<br>"; Output: Enter
Second instantiation of Object
$obj 2 = new name ("PHP");
Performing printing
$obj 2->printname (); Output: PHP
?>
Description: The above class uses the this pointer respectively in the statement ① and the statement ②, then who is this pointing to? In fact this is when the instantiation to determine who to point to, such as the first instantiation of the object (statement ③), then this is to point to $obj1 object, then execute statement ② print ($this-><name) into print ($ Obj1t->name), then of course the "Pbphome" is exported. In the second instance, print ($this->name) becomes print ($obj 2->name), and "PHP" is exported. So, this is a pointer to the current object instance and does not point to any other object or class.
{two}. PHP this,self,parent The difference between the two self article
In this article we will explain the use of self
First, let's be clear that self is pointing to the class itself, which is that self does not point to any object that has already been instantiated, and general self uses it to point to static variables in the class. If we use the static (generally keyword static) members of a class, we must also use self to invoke it. Also note that using self to invoke a static variable must use:: (field operator symbol), see instance.
Copy Code code as follows:
<?php
Class counter//Definition of a counter
{
Define attributes, including a static variable $firstcount, and assign an initial value of 0 statements ①
private static $firstCount = 0;
Private $lastCount;
Constructors
function __construct ()
{
$this->lastcount = ++self:: $firstCount; Use self to invoke static variable statements ②
}
Print Lastcount values
function Printlastcount ()
{
Print ($this->lastcount);
}
}
Instantiating an Object
$obj = new Counter ();
$obj->printlastcount (); When executed here, the program outputs 1
?>
Note here Two local statement ① and statement ②. We define a static variable $firstcount in the statement ①, and then we use self to call this value when the statement ②, then we call the static variable $frestcount defined by the class itself. Our static variable has nothing to do with the instance of the following object, it's just about the class, so I call the class itself, so we can't use this to refer to because self is pointing to the class itself, regardless of any object instance. And then the previous use of this call is the instantiated object $obj, we don't confuse it.
As for self, it is easier to understand the combination of examples. The end of the second article.
{Three}php of this,self,parent in three parent articles
We will explain the usage of parent in this article.
First, we are clear that parent is a pointer to the parent class, and we typically use parent to invoke the constructor of the parent class. Examples are as follows:
Copy Code code as follows:
<?php
Establish base class animal
Class Animal
{
Public $name; The properties of the base class, named $name
constructor of the base class, initializing the assignment
Public function __construct ($name)
{
$this->name = $name;
}
}
To define a derived class person to inherit from the animal class
Class Person extends Animal
{
Public $personSex; For derived classes, new attributes are defined $personsex sex, $personAge age
Public $personAge;
Constructors for derived classes
function __construct ($personSex, $personAge)
{
Parent::__construct ("Pbphome"); The constructor statement for the parent class was invoked using parent ①
$this->personsex = $personSex;
$this->personage = $personAge;
}
A member function of a derived class for printing, formatting: 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");
Performing printing
$personObject->printperson (); Output result: Pbphome is Male,age is 21
?>
It also contains the use of this, we analyze ourselves. We note this detail: member properties are public (publicly available properties and methods, both inside and outside of the class are accessible), especially for the parent class, which is for the inheriting class to access through this. The key point is in the statement ①: Parent::__construct ("Heiyeluren"), when we use parent to invoke the constructor of the parent class to initialize the parent class, so that the object of the inheriting class is assigned the name of Pbphome. We can test and then instantiate an object $personobject1, and the name is still pbphome after the print is executed.
Summary: This is a pointer to an object instance, which is determined when instantiated, and 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 superclass, which typically uses parent to invoke the constructor of the superclass.