PHP5 is a language that has most of the features of an object-oriented language. It has many more object-oriented features than PHP4, but some concepts are a bit confusing, so I will talk about it today. I am not good at explaining it. Please forgive me, experts. . (To read this article, you need to understand the object-oriented knowledge of PHP5)
First, let’s understand the above three keywords: this, self, parent, which is easier to understand literally, refers to this, self, father, haha, it’s more fun. , let’s first establish a few concepts. Where are these three keywords used? Let’s briefly explain that this is a pointer to the current object (let’s use the pointer in C to look at it), and self is a pointer to the current object. A pointer to a class, parent is a pointer to the parent class. We frequently use pointers to describe here because there is no better language to express it. Haha, I didn’t learn Chinese well. -_-#
If you don’t understand it well in this way, let’s talk about it based on actual examples.
(1) this
1 <?php
2
3 class UserName
4 {
5 //Define attributes
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 //Print username member function
18 function printName()
19 {
20 print($this->name); //This pointer is used again
twenty one }
twenty two }
twenty three
24 //instantiate object
25 $nameObject = new UserName( "heiyeluren" );
26
27 //Execute printing
28 $nameObject->printName(); //Output: heiyeluren
29
30 //Second instantiation of object
31 $nameObject2 = new UserName( "PHP5" );
32
33 //Execute printing
34 $nameObject2->printName(); //Output: PHP5
35 ?>
Let us see that the above class uses this pointer on lines 11 and 20 respectively, so who does this point 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, print( $this -><name) becomes print($nameObject->name), then of course "heiyeluren" is output. In the second instance, print( $this->name ) becomes print( $nameObject2->name ), so "PHP5" is output. Therefore, 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 must 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 <?php
2
3 class Counter
4 {
5 //Define properties, including a static variable
6 private static $firstCount = 0;
7 private $lastCount;
8
9 //Constructor
10 function __construct()
11 {
12 $this->lastCount = ++selft::$firstCount; //Use self to call static variables. When calling self, you must use:: (field operation symbol)
13}
14
15 //Print the latest value
16 function printLastCount()
17 {
18 print( $this->lastCount );
19}
20}
twenty one
22 //instantiate object
23 $countObject = new Counter();
twenty four
25 $countObject->printLastCount(); //Output 1
26
27 ?>
We only need to pay attention to two places here, line 6 and line 12. 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 it, and using "::" to connect in the middle, which is what we call domain operator, then 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 if I call the class itself, then we cannot Use this to reference, you 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.
1 <?php
2
3 //base class
4 classes Animal
5 {
6 //Attributes of base class
7 public $name; //name
8
9 //Constructor of base class
10 public function __construct( $name )
11 {
12 $this->name = $name;
13}
14}
15
16 // Derived class
17 class Person extends Animal //Person class inherits Animal class
18 {
19 public $personSex; //Gender
20 public $personAge; //Age
twenty one
22 //Constructor of inherited class
23 function __construct( $personSex, $personAge )
twenty four {
25 parent::__construct( "heiyeluren" ); //Use parent to call the constructor of the parent class
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 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 the following 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 members of the parent class are all public. So we can directly use this to call in the inherited class.
Summary:
this is a pointer to an object instance, self is a reference to the class itself, and parent is a reference to the parent class.
Basically, that's all I know. There must be some misunderstandings. Please point them out!
My email: [email protected]
WriteTime: 2004-11-3 18:30
http://dev.csdn.net/author/heiyeshuwu/702e33d6abaf4be58c06f1b55cf0fc8c.html