码迷,mamicode.com
首页 > Web开发 > 详细

【PHP】this,self,parent的区别(转)

时间:2014-12-03 20:50:52      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   使用   

一. 定义&区别


self:  指向当前类的指针,self是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量。

this:  指向当前对象的指针,使用parent来调用父类的构造函数。

parent:  指向父类的指针

 

二.使用区别


 1.self

bubuko.com,布布扣
<?php

    classcounter     //定义一个counter的类
    {
        //定义属性,包括一个静态变量$firstCount,并赋初值0 语句①  
        private static $firstCount = 0;
        private $lastCount;

        //构造函数
        function __construct()
        {
             $this->lastCount =++self::$firstCount;      //使用self来调用静态变量 语句②
        }

        //打印lastCount数值
        function printLastCount()
        {
             print( $this->lastCount );
        }
    }

 //实例化对象
  $obj = new Counter();

 $obj->printLastCount();                             //执行到这里的时候,程序输出1

 ?>
View Code

这里要注意两个地方语句①和语句②。我们在语句①定义了一个静态变量$firstCount,那么在语句②的时候使用了self调用这个值,那么这时候我们调用的就是类自己定义的静态变量$frestCount。我们的静态变量与下面对象的实例无关,它只是跟类有关,那么我调用类本身的的,那么我们就无法使用this来引用,因为self是指向类本身,与任何对象实例无关。然后前面使用的this调用的是实例化的对象$obj,大家不要混淆了。

 

2.this

bubuko.com,布布扣
<?php

 classname          //建立了一个名为name的类
 {
    private$name;         //定义属性,私有

    //定义构造函数,用于初始化赋值
    function __construct( $name )
    {
         $this->name =$name;         //这里已经使用了this指针语句①
    }

    //析构函数
    function __destruct(){}

    //打印用户名成员函数
    function printname()
    {
         print( $this->name);             //再次使用了this指针语句②,也可以使用echo输出
    }
 }
 $obj1 = new name("PBPHome");   //实例化对象 语句③

 //执行打印
 $obj1->printname(); //输出:PBPHome
 echo"<br>";                                    //输出:回车

 //第二次实例化对象
 $obj2 = new name( "PHP" );

 //执行打印
 $obj2->printname();                         //输出:PHP
 ?>
View Code

说明:上面的类分别在 语句①和语句②使用了this指针,那么当时this是指向谁呢?其实this是在实例化的时候来确定指向谁,比如第一次实例化对象的时候(语句③),那么当时this就是指向$obj1对象,那么执行语句②的打印时就把print( $this-><name ) 变成了 print($obj1t->name ),那么当然就输出了"PBPHome"。第二个实例的时候,print($this->name )变成了print( $obj2->name),于是就输出了"PHP"。所以说,this就是指向当前对象实例的指针,不指向任何其他对象或类。

 

3.parent

bubuko.com,布布扣
<?php
 //建立基类Animal
 class Animal
 {
    public $name; //基类的属性,名字$name

    //基类的构造函数,初始化赋值
    public function __construct( $name )
    {
         $this->name = $name;
    }
 }

 //定义派生类Person 继承自Animal类
 class Person extends Animal
 {
    public$personSex;       //对于派生类,新定义了属性$personSex性别、$personAge年龄
    public $personAge;

    //派生类的构造函数
    function __construct( $personSex, $personAge )
    {
         parent::__construct( "PBPHome");    //使用parent调用了父类的构造函数 语句①
         $this->personSex = $personSex;
         $this->personAge = $personAge;
    }

    //派生类的成员函数,用于打印,格式:名字 is name,age is 年龄
    function printPerson()
    {
         print( $this->name. " is ".$this->personSex. ",age is ".$this->personAge );
     }
 }

 //实例化Person对象
 $personObject = new Person( "male", "21");

 //执行打印
 $personObject->printPerson();//输出结果:PBPHome is male,age is 21

 ?>
View Code

 

 里面同样含有this的用法,大家自己分析。我们注意这么个细节:成员属性都是public(公有属性和方法,类内部和外部的代码均可访问)的,特别是父类的,这是为了供继承类通过this来访问。关键点在语句①:parent::__construct( "heiyeluren"),这时候我们就使用parent来调用父类的构造函数进行对父类的初始化,这样,继承类的对象就都给赋值了name为PBPHome。我们可以测试下,再实例化一个对象$personObject1,执行打印后name仍然是PBPHome。

 

三.总结


this是指向对象实例的一个指针,在实例化的时候来确定指向;self是对类本身的一个引用,一般用来指向类中的静态变量;parent是对父类的引用,一般使用parent来调用父类的构造函数。

this是指向对象实例的一个指针,self是对类本身的一个引用,parent是对父类的引用。

 

【PHP】this,self,parent的区别(转)

标签:des   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/huxiaoyun90/p/4141079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!