大家也许多首先我们来理解三个关键字: this,self,parent,从字面上比较好理解,是指这,自己,父亲,呵呵,比较好玩了,我们先建立几个概念,这三个关键字分别是用在什么地方 呢?我们初步解释一下,this是指向当前对象的指针(我们姑且用C里面的指针来看吧),self是指向当前类的指针,parent是指向父类的指针。
这么说还不能很了解,那我们就根据实际的例子结合来讲讲。
(1) PHP5指针之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> <li class=""><span> </span></li> <li class="alt"><span>class UserName </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>//定义属性 </span></li> <li class=""><span>private $name; </span></li> <li class="alt"><span> </span></li> <li class=""><span>//定义构造函数 </span></li> <li class="alt"><span>function __construct( $name ) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$this-</span><span class="tag"><strong><font color="#006699">></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>
我们看,上面的类分别在11行和20行使用了this指针,那么当时this是指向谁呢?其实this是在实例化的时候来确定指向谁,比如第一次实例化对象 的时候(25行),那么当时this就是指向$nameObject对象,那么执行18行的打印的时候就把print( $this->
(2)PHP5指针之self
首先我们要明确一点,self是指向类本身,也就是self是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量。
<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> <li class=""><span> </span></li> <li class="alt"><span>class Counter </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>//定义属性,包括一个静态变量 </span></li> <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> </li> <li class="alt"><span>private $lastCount; </span></li> <li class=""><span>//构造函数 </span></li> <li class="alt"><span>function __construct() </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$this-</span><span class="tag"><strong><font color="#006699">></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>
我们这里只要注意两个地方,第6行和第12行。我们在第二行定义了一个静态变量$firstCount,并且初始值为0,那么在12行的时候调用了这个值 得,使用的是self来调用,并且中间使用"::"来连接,就是我们所谓的域运算符,那么这时候我们调用的就是类自己定义的静态变量$ frestCount,我们的静态变量与下面对象的实例无关,它只是跟类有关,那么我调用类本身的的,那么我们就无法使用this来引用,可以使用 self来引用,因为self是指向类本身,与任何对象实例无关。换句话说,假如我们的类里面静态的成员,我们也必须使用self来调用。
(3)PHP5指针之parent
我们知道parent是指向父类的指针,一般我们使用parent来调用父类的构造函数。
<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> <li class=""><span>//基类 </span></li> <li class="alt"><span>class Animal </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>//基类的属性 </span></li> <li class=""><span>public $name; //名字 </span></li> <li class="alt"><span>//基类的构造函数 </span></li> <li class=""><span>public function __construct( $name ) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$this-</span><span class="tag"><strong><font color="#006699">></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>我们注意这么几个细节:成员属性都是public的,特别是父类的,是为了供继承类通过this来访问。我们注意关键的地方,第25行:parent:: __construct( "heiyeluren" ),这时候我们就使用PHP5指针中的parent来调用父类的构造函数进行对父类的初始化,因为父类的成员都是public的,于是我们就能够在继承类中直接使用 this来调用。