php中this/self/parent三个关键字用法说明

  this,self,parent三个关键字之间的区别。从字面上比较好理解,分别是指这、自己、父亲。我们先建立几个概念,这三个关键字分别是用在什么地方呢?我们初步解释一下,this是指向当前对象的指针(姑且用C里面的指针来看吧),self是指向当前类的指针,parent是指向父类的指针。我们这里频繁使用指针来描述,是因为没有更好的语言来表达。

      这么说还不能很了解,那我们就根据实际的例子结合来讲讲:

Php代码 复制代码
  1. (1) this   
  2.   
  3.  class UserName   
  4.  {   
  5.      //定义属性      
  6.      private $name;   
  7.   
  8.      //定义构造函数   
  9.      function __construct( $name )   
  10.      {   
  11.           $this->name = $name//这里已经使用了this指针   
  12.      }   
  13.   
  14.      //析构函数   
  15.      function __destruct(){}   
  16.   
  17.      //打印用户名成员函数   
  18.      function printName()   
  19.      {   
  20.          print( $this->name ); //又使用了this指针   
  21.      }   
  22.  }   
  23.   
  24.  //实例化对象   
  25.  $nameObject = new UserName( "heiyeluren" );   
  26.   
  27.  //执行打印   
  28.  $nameObject->printName(); //输出: heiyeluren   
  29.   
  30.  //第二次实例化对象   
  31.  $nameObject2 = new UserName( "PHP5" );   
  32.   
  33.  //执行打印   
  34.  $nameObject2->printName(); //输出:PHP5   
  35.  ?>   
  36.   
  37. 我们看,上面的类分别在11行和20行使用了this指针,那么当时this是指向谁呢?其实this是在实例化的时候来确定指向谁,比如第一次实例化对象的时候(25行),那么当时this就是指向$nameObject对象,那么执行18行的打印的时候就把print( $this-><name )变成了print( $nameObject->name ),那么当然就输出了"heiyeluren"。第二个实例的时候,print( $this->name )变成了print( $nameObject2->name ),于是就输出了"PHP5"。所以说,this就是指向当前对象实例的指针,不指向任何其他对象或类。   
  38.   
  39.     
  40.   
  41. (2)self   
  42.   
  43. 首先我们要明确一点,self是指向类本身,也就是self是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量。   
  44.   
  45.  <?php   
  46.   
  47.      class Counter   
  48.      {   
  49.          //定义属性,包括一个静态变量   
  50.          private static $firstCount = 0;   
  51.          private $lastCount;   
  52.   
  53.          //构造函数   
  54.          function __construct()   
  55.          {   
  56.               $this->lastCount = ++selft::$firstCount//使用self来调用静态变量,使用self调用必须使用::(域运算符号)   
  57.          }   
  58.   
  59.          //打印最次数值   
  60.          function printLastCount()   
  61.          {   
  62.               print( $this->lastCount );   
  63.          }   
  64.      }   
  65.   
  66.  //实例化对象   
  67.  $countObject = new Counter();   
  68.   
  69.  $countObject->printLastCount(); //输出 1   
  70.   
  71.  ?>   
  72.   
  73. 我们这里只要注意两个地方,第6行和第12行。我们在第二行定义了一个静态变量$firstCount,并且初始值为0,那么在12行的时候调用了这个值,使用的是self来调用,并且中间使用"::"来连接,就是我们所谓的域运算符,那么这时候我们调用的就是类自己定义的静态变量$frestCount,我们的静态变量与下面对象的实例无关,它只是跟类有关,那么我调用类本身的的,那么我们就无法使用this来引用,可以使用 self来引用,因为self是指向类本身,与任何对象实例无关。换句话说,假如我们的类里面静态的成员,我们也必须使用self来调用。   
  74.   
  75.   
  76. (3)parent   
  77.   
  78. 我们知道parent是指向父类的指针,一般我们使用parent来调用父类的构造函数。   
  79.   
  80.  <?php   
  81.   
  82.  //基类   
  83.  class Animal   
  84.  {   
  85.      //基类的属性   
  86.      public $name//名字   
  87.   
  88.      //基类的构造函数   
  89.      public function __construct( $name )   
  90.      {   
  91.           $this->name = $name;   
  92.      }   
  93.  }   
  94.   
  95.  //派生类   
  96.  class Person extends Animal //Person类继承了Animal类   
  97.  {   
  98.      public $personSex//性别   
  99.      public $personAge//年龄   
  100.   
  101.      //继承类的构造函数   
  102.      function __construct( $personSex$personAge )   
  103.      {   
  104.           parent::__construct( "heiyeluren" ); //使用parent调用了父类的构造函数   
  105.           $this->personSex = $personSex;   
  106.           $this->personAge = $personAge;   
  107.      }   
  108.   
  109.      function printPerson()   
  110.      {   
  111.           print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );   
  112.       }   
  113.  }   
  114.   
  115.  //实例化Person对象   
  116.  $personObject = new Person( "male""21");   
  117.   
  118.  //执行打印   
  119.  $personObject->printPerson(); //输出:heiyeluren is male,this year 21   
  120.   
  121.  ?>  
(1) this

 class UserName
 {
     //定义属性   
     private $name;

     //定义构造函数
     function __construct( $name )
     {
          $this->name = $name; //这里已经使用了this指针
     }

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

     //打印用户名成员函数
     function printName()
     {
         print( $this->name ); //又使用了this指针
     }
 }

 //实例化对象
 $nameObject = new UserName( "heiyeluren" );

 //执行打印
 $nameObject->printName(); //输出: heiyeluren

 //第二次实例化对象
 $nameObject2 = new UserName( "PHP5" );

 //执行打印
 $nameObject2->printName(); //输出:PHP5
 ?>

我们看,上面的类分别在11行和20行使用了this指针,那么当时this是指向谁呢?其实this是在实例化的时候来确定指向谁,比如第一次实例化对象的时候(25行),那么当时this就是指向$nameObject对象,那么执行18行的打印的时候就把print( $this-><name )变成了print( $nameObject->name ),那么当然就输出了"heiyeluren"。第二个实例的时候,print( $this->name )变成了print( $nameObject2->name ),于是就输出了"PHP5"。所以说,this就是指向当前对象实例的指针,不指向任何其他对象或类。

 

(2)self

首先我们要明确一点,self是指向类本身,也就是self是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量。

 <?php

     class Counter
     {
         //定义属性,包括一个静态变量
         private static $firstCount = 0;
         private $lastCount;

         //构造函数
         function __construct()
         {
              $this->lastCount = ++selft::$firstCount; //使用self来调用静态变量,使用self调用必须使用::(域运算符号)
         }

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

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

 $countObject->printLastCount(); //输出 1

 ?>

我们这里只要注意两个地方,第6行和第12行。我们在第二行定义了一个静态变量$firstCount,并且初始值为0,那么在12行的时候调用了这个值,使用的是self来调用,并且中间使用"::"来连接,就是我们所谓的域运算符,那么这时候我们调用的就是类自己定义的静态变量$frestCount,我们的静态变量与下面对象的实例无关,它只是跟类有关,那么我调用类本身的的,那么我们就无法使用this来引用,可以使用 self来引用,因为self是指向类本身,与任何对象实例无关。换句话说,假如我们的类里面静态的成员,我们也必须使用self来调用。


(3)parent

我们知道parent是指向父类的指针,一般我们使用parent来调用父类的构造函数。

 <?php

 //基类
 class Animal
 {
     //基类的属性
     public $name; //名字

     //基类的构造函数
     public function __construct( $name )
     {
          $this->name = $name;
     }
 }

 //派生类
 class Person extends Animal //Person类继承了Animal类
 {
     public $personSex; //性别
     public $personAge; //年龄

     //继承类的构造函数
     function __construct( $personSex, $personAge )
     {
          parent::__construct( "heiyeluren" ); //使用parent调用了父类的构造函数
          $this->personSex = $personSex;
          $this->personAge = $personAge;
     }

     function printPerson()
     {
          print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );
      }
 }

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

 //执行打印
 $personObject->printPerson(); //输出:heiyeluren is male,this year 21

 ?>



 

 

Php代码 复制代码
  1. 成员属性都是public的,特别是父类的,是为了供继承类通过this来访问。我们注意关键的地方,第25行:   
  2. parent::__construct( "heiyeluren"  
  3. ),这时候我们就使用parent来调用父类的构造函数进行对父类的初始化,因为父类的成员都是public的,于是我们就能够在继承类中直接使用   
  4. this来调用。   
  5.   
  6.   
  7. 总结:   
  8. this是指向对象实例的一个指针,self是对类本身的一个引用,parent是对父类的引用。  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值