Difference and comparison of the three keywords this, self and parent in php

WBOY
Release: 2016-08-08 09:30:55
Original
993 people have browsed it
The three keywords this, self, and parent are easier to understand literally, referring to this, self, and father respectively.
this is the pointer to the current object (let’s use the pointer in C to look at it)
self is the pointer to the current class
parent is the pointer to the parent class (we frequently use pointers to describe here, Because there is no better language to express it)
Let’s take a look based on actual examples
(1) this
1
2
3 class UserName
4 {
5 // Define member properties
6 private $name;
7
8 // Define constructor
9 function __construct( $name )
10 {
11 $ this->name = $name; //This pointer has been used here
12 }
13
14 //Destructor
15 ​​function __destruct(){}
16
17                                                                                                                                                                               Member function
              function printName()
19                                                                  ; }
22 }
23
24 //Instantiate the object
25 $nameObject = new UserName( "heiyeluren" );
26
27 //Perform printing
28 $name Object ->printName(); //Output: heiyeluren
29
30 //Second instantiation of the object
31 $nameObject2 = new UserName( "PHP5" );
32
33 //Execute printing
34 $nameObject2->printName(); //Output: PHP5
35 ?>
We see that the above class uses this pointer on lines 11 and 20 respectively. , so who was this pointing to at that time?
In fact, this determines who it points to when instantiating it. For example, when the object is instantiated for the first time (line 25),
then this points to the $nameObject object, then when printing on line 18 is executed Just change print( $this->name ) into print( $nameObject->name ), then of course "heiyeluren" will be output.
In the second instance, print( $this- >name ) becomes print( $nameObject2->name ), so "PHP5" is output.
So, this is a pointer to the current object instance and does not point to any other object or class.
(2)self
First of all, we have to make it clear that self points to the class itself, that is, self does not point to any instantiated object. Generally, self is used to point to static variables in the class.
1
2
3 class Counter
4 {
5 //Define attributes, including a static variable
6 private static $ firstCount = 0;
7          private $lastCount;
8
9                                                                                                                                                            $this->lastCount = ++selft::$firstCount; / /Use self to call static variables. When calling self, you must use:: (field operation symbol)
13                                                                                                                                                         18
22 //Instantiate the object
23 $countObject = new Counter() ;
24
25 $countObject->printLastCount(); //Output 1
26
27 ?>
We only need to pay attention to two places here, line 6 and line 12 OK.
We defined a static variable $firstCount on the second line, and the initial value is 0. Then we called this value on line 12, using self to call, and using "::" to connect in the middle.
is what we call a domain operator, so what we call at this time is the static variable $frestCount defined by the class itself. Our static variable has nothing to do with the instance of the following object, it is only related to the class,
Then I call The class itself, then we cannot use this to reference, we can use self to reference,
because self points to the class itself and has nothing to do with any object instance. In other words, if there are static members in our class, we must also use self to call them.
(3)parent
We know that parent is a pointer to the parent class. Generally, we use parent to call the constructor of the parent class. / /Name
8
9 // Base class constructor
10 public function __construct( $name )
11 {
12 $this->name = $name;
" // Gender
20 public $personAge; //Age
21
22 //Constructor of inherited class
23 function __construct( $personSex, $personAge )
24 {
25 parent ::__construct( "heiyeluren" ); //The constructor of the parent class is called using parent
26 $this->personSex = $personSex;
27 $this->personAge = $personAge;
28 }
29
30 function printPerson()
31 {
32 print( $this->name. " is " .$this->personSex. ", this year ". $this->personAge );
33 }
34 }
35
36 //Instantiate the Person object
37 $personObject = new Person( "male", "21") ;
38
39 //Execute printing
40 $personObject->printPerson(); //Output: heiyeluren is male, this year 21
41
42 ?>
We pay attention to these few details: member attributes are all public, especially those of the parent class, for inherited classes to access through this.
We pay attention to the key point, line 25: parent::__construct( "heiyeluren" ), at this time we use parent to call the constructor of the parent class to initialize the parent class,
Because the parent class The members are all public, so we can directly use this to call in the inherited class.
Summary:
this is a pointer to the object instance, self is a reference to the class itself, and parent is a reference to the parent class.

Reprinted from: http://blog.csdn.net/skynet001/article/details/7518164
The above introduces the distinction and comparison of the three keywords this, self, and parent in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template