php5| Keywords |
PHP5 is a language with most object-oriented language features, than PHP4 has a lot of object-oriented characteristics, but some of the concept is also more around people, so today take out to say, say, please master forgive me. (Read this article, need to understand PHP5 object-oriented knowledge)
First of all, we have to understand the above three keywords: this,self,parent, literally better understanding, refers to this, his father, oh, more fun, we first set up a few concepts, these three key words are used in what place? Let us explain briefly This is a pointer to the current object (let's look at the pointer in c), self is a pointer to the current class, and parent is a pointer to the parent class. We often use pointers here to describe, because there is no better language to express, hehe, Chinese did not learn. -_-#
So it's not very well understood, so let's talk about it in terms of actual examples.
(1) This
1 <?php
2
3 Class UserName
4 {
5//Definition Properties
6 private $name;
7
8//Defining constructors
9 function __construct ($name)
10 {
One $this->name = $name; The this pointer is already used here
12}
13
14//destructor
function __destruct () {}
16
17//Print User name member function
The function printname ()
19 {
Print ($this->name); This pointer is also used
21}
22}
23
24//Materialized objects
$nameObject = new UserName ("Heiyeluren");
26
27//Performing printing
$nameObject->printname (); Output: Heiyeluren
29
30//second instantiation of object
$nameObject 2 = new UserName ("PHP5");
32
33//Performing printing
$nameObject 2->printname (); Output: PHP5
Km?>
Let's see, the class above uses the this pointer on lines 11 and 20 respectively, so 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 (25 lines), then this is to point to the $nameobject object, then perform 18 lines of printing when the print ($this-><name) becomes print ($nameObject->name), then of course the "Heiyeluren" is exported. In the second instance, print ($this->name) becomes print ($nameObject 2->name), and the "PHP5" is exported. So, this is a pointer to the current object instance and does not point to any other object or class.
(2) 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.
1 <?php
2
3 Class Counter
4 {
5//define attributes, including a static variable
6 private static $firstCount = 0;
7 Private $lastCount;
8
9//constructor
Ten function __construct ()
11 {
$this->lastcount = ++selft:: $firstCount; Use self to invoke a static variable that must be invoked using the Self::(field operator symbol)
13}
14
15//Print the most time value
function Printlastcount ()
17 {
Print ($this->lastcount);
19}
20}
21st
22//Materialized objects
$countObject = new Counter ();
24
$countObject->printlastcount (); Output 1
26
?>
Here we just need to pay attention to two places, lines 6th and 12th. We define a static variable $firstcount in the second line, and the initial value is 0, then we call this worth on line 12, using self to invoke, and the middle uses "::" To connect, which is what we call a domain operator, So what we're calling is the static variable $frestcount that the class itself defines, and our static variable has nothing to do with the instance of the following object, it's just about the class, and then I call the class itself, so we can't use this to refer to, you can use self to refer to, Because self is pointing to the class itself, it is independent of any object instance. In other words, if the static members of our class, we must also use self to invoke.
(3) Parent
We know that parent is a pointer to a parent class, and generally we use parent to invoke the constructor of the parent class.
1 <?php
2
3//base class
4 Class Animal
5 {
6//Base class properties
7 public $name; Name
8
9//Base class constructor
Ten public Function __construct ($name)
11 {
$this->name = $name;
13}
14}
15
16//derived class
Class Person extends Animal//person classes inherit the Animal class
18 {
Public $personSex; Gender
Public $personAge; Age
21st
22//Inheritance class constructor
function __construct ($personSex, $personAge)
24 {
Parent::__construct ("Heiyeluren"); The constructor of the parent class was called using parent
$this->personsex = $personSex;
$this->personage = $personAge;
28}
29
function Printperson ()
31 {
Print ($this->name. ' is '. $this->personsex. ", this year". $this->personage);
33}
34}
35
36//Instantiate Person object
Panax Notoginseng $personObject = new Person ("male", "21");
38
39//Performing printing
$personObject->printperson (); Output: Heiyeluren is male,this year 21
41
?>
We note a few details: the member properties are public, especially the parent class, for the inheriting class to access through this. We pay attention to the key, line 25th: parent::__construct ("Heiyeluren"), at which point we use parent to invoke the constructor of the parent class to initialize the parent class because the members of the parent class are public. So we can invoke it directly in the inheritance class using this.
Summarize:
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 I know so much, there must be some understanding of the wrong place, please point out the master!
My e-mail: heiyeluren@163.com
Writetime:2004-11-3 18:30