Home >Backend Development >PHP Tutorial >Introduction to the categories of PHP5 pointers_PHP tutorial
There are many of you First of all, let’s understand the 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.
It’s not very clear yet, so let’s talk about it based on actual examples.
(1) PHP5 pointer this
<ol class="dp-xml"> <li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>class UserName </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>//定义属性 </SPAN><LI class=""><SPAN>private $name; </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//定义构造函数 </SPAN><LI class=alt><SPAN>function __construct( $name ) </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>$this-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span class="attribute"><font color="#ff0000">name</font></span><span> = $name; //这里已经使用了this指针 </span> </li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>//析构函数 </span></li> <li class="alt"><span>function __destruct(){} </span></li> <li class=""><span> </span></li> <li class="alt"><span>//打印用户名成员函数 </span></li> <li class=""><span>function printName() </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>print( $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>name ); //又使用了this指针 </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>//实例化对象 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">nameObject</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> UserName( "heiyeluren" ); </span> </li> <li class=""><span> </span></li> <li class="alt"><span>//执行打印 </span></li> <li class=""> <span>$nameObject-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>printName(); //输出: heiyeluren </span> </li> <li class="alt"><span> </span></li> <li class=""><span>//第二次实例化对象 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">nameObject2</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> UserName( "PHP5" ); </span> </li> <li class=""><span> </span></li> <li class="alt"><span>//执行打印 </span></li> <li class=""> <span>$nameObject2-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>printName(); //输出:PHP5 </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
We 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 ->
(2) PHP5 pointer 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.
<ol class="dp-xml"> <li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>class Counter </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>//定义属性,包括一个静态变量 </SPAN><LI class=""><SPAN>private static $</SPAN><SPAN class=attribute><FONT color=#ff0000>firstCount</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>0</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>private $lastCount; </SPAN><LI class=""><SPAN>//构造函数 </SPAN><LI class=alt><SPAN>function __construct() </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>$this-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span class="attribute"><font color="#ff0000">lastCount</font></span><span> = ++selft::$firstCount; //使用self来调用静态变量,使用self调用必须使用::(域运算符号) </span> </li> <li class=""><span>} </span></li> <li class="alt"><span>//打印最次数值 </span></li> <li class=""><span>function printLastCount() </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>print( $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>lastCount ); </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>//实例化对象 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">countObject</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Counter(); </span> </li> <li class=""> <span>$countObject-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>printLastCount(); //输出 1 </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
We only need to pay attention to two places here, lines 6 and 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) PHP5 pointer parent
We know that parent is a pointer to the parent class. Generally, we use parent to call the constructor of the parent class.
<ol class="dp-xml"> <li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>//基类 </SPAN><LI class=alt><SPAN>class Animal </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>//基类的属性 </SPAN><LI class=""><SPAN>public $name; //名字 </SPAN><LI class=alt><SPAN>//基类的构造函数 </SPAN><LI class=""><SPAN>public function __construct( $name ) </SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><SPAN>$this-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span class="attribute"><font color="#ff0000">name</font></span><span> = $name; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>//派生类 </span></li> <li class=""><span>class Person extends Animal //Person类继承了Animal类 </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>public $personSex; //性别 </span></li> <li class="alt"><span>public $personAge; //年龄 </span></li> <li class=""><span>//继承类的构造函数 </span></li> <li class="alt"><span>function __construct( $personSex, $personAge ) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>parent::__construct( "heiyeluren" ); //使用parent调用了父类的构造函数 </span></li> <li class=""> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">personSex</font></span><span> = $personSex; </span> </li> <li class="alt"> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">personAge</font></span><span> = $personAge; </span> </li> <li class=""><span>} </span></li> <li class="alt"><span>function printPerson() </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>print( $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>name. " is " .$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>personSex. ",this year " .$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>personAge ); </span> </li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>//实例化Person对象 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">personObject</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Person( "male", "21"); </span> </li> <li class=""><span> </span></li> <li class="alt"><span>//执行打印 </span></li> <li class=""> <span>$personObject-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>printPerson(); //输出:heiyeluren is male,this year 21 </span> </li> <li class="alt"><span> </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>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 the parent in the PHP5 pointer to call the constructor of the parent class to initialize the parent class, because the members of the parent class are It is public, so we can directly use this to call it in the inherited class.