php中this,self,parent三个关键字的区别辨析

一、总结

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

1、self关键字使用的时候注意什么?

self是小写,而不是大写,如果大写的话表示SELF这个类

12 $this->lastCount = ++self::$firstCount; //使用self来调用静态变量,使用self调用必须使用::(域运算符号)

2、self和this的区别是什么?

self相当于是静态,this相当于是非静态

self代表的是本类:便于操作静态方法和属性(静态)

this代表的是本对象:便于操作类的对象的属性和方法(非静态)

5 //定义属性,包括一个静态变量
6 private static $firstCount = 0;
7 private $lastCount;
8
9 //构造函数
10 function __construct()
11 {
12 $this->lastCount = ++self::$firstCount; //使用self来调用静态变量,使用self调用必须使用::(域运算符号)
13 }
14
15 //打印最次数值
16 function printLastCount()
17 {
18 print( $this->lastCount );
19 }

3、self和parent的区别和联系是什么?

都是代表类

self代表本类:用途一般为本类中调用自己的方法或者属性(这里的方法和属性一般是静态的)

parent代表父类:用途一般为子类继承父类的构造函数是使用

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

二、php中this,self,parent三个关键字

this,self,parent三个关键字从字面上比较好理解,分别是指这、自己、父亲。

this是指向当前对象的指针(姑且用C里面的指针来看吧)
self是指向当前类的指针
parent是指向父类的指针(我 们这里频繁使用指针来描述,是因为没有更好的语言来表达)

根据实际的例子来看看

(1) this

1 <?php
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 ?>

我 们看,上面的类分别在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使用来指向类中的静态变量。

1 <?php
2
3 class Counter
4 {
5 //定义属性,包括一个静态变量
6 private static $firstCount = 0;
7 private $lastCount;
8
9 //构造函数
10 function __construct()
11 {
12 $this->lastCount = ++self::$firstCount; //使用self来调用静态变量,使用self调用必须使用::(域运算符号)
13 }
14
15 //打印最次数值
16 function printLastCount()
17 {
18 print( $this->lastCount );
19 }
20 }
21
22 //实例化对象
23 $countObject = new Counter();
24
25 $countObject->printLastCount(); //输出 1
26
27 ?>

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

(3)parent

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

1 <?php
2
3 //基类
4 class Animal
5 {
6 //基类的属性
7 public $name; //名字
8
9 //基类的构造函数
10 public function __construct( $name )
11 {
12 $this->name = $name;
13 }
14 }
15
16 //派生类
17 class Person extends Animal //Person类继承了Animal类
18 {
19 public $personSex; //性别
20 public $personAge; //年龄
21
22 //继承类的构造函数
23 function __construct( $personSex, $personAge )
24 {
25 parent::__construct( "heiyeluren" ); //使用parent调用了父类的构造函数
26 $this->personSex = $personSex;
27 $this->personAge = $personAge;
28 }
29
30 function printPerson()
31 {
32 print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );
33 }
34 }
35
36 //实例化Person对象
37 $personObject = new Person( "male", "21");
38
39 //执行打印
40 $personObject->printPerson(); //输出:heiyeluren is male,this year 21
41
42 ?>

我 们注意这么几个细节:成员属性都是public的,特别是父类的,是为了供继承类通过this来访问。
我们注意关键的地方,第25行: parent::__construct( "heiyeluren" ),这时候我们就使用parent来调用父类的构造函数进行对父类的初始化,
因为父类的成员都是public的,于是我们就能够在继承类中直接使用 this来调用。

总结:

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

 
参考:php中this,self,parent三个关键字 - CSDN博客
https://blog.csdn.net/skynet001/article/details/7518164
 
 

最新文章

  1. OracleDBA之表管理
  2. 【原】iOS学习47之第三方-FMDB
  3. css样式—字体垂直、水平居中
  4. Win10下E3-1231 V3开启Intel虚拟化技术(vt-x)安装HAXM
  5. [转]android访问网络:java.net.ConnectException: localhost/127.0.0.1:8888 - Connection refused
  6. 如何使用批处理解决批量telnet命令的输入
  7. asp.net读excle的数据类型不统一取出空值问题
  8. (原创)speex与wav格式音频文件的互相转换
  9. ☀【CSS3】形状
  10. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列
  11. 怎样从一个DLL中导出一个C++类
  12. mybatis-generator 代码自动生成工具(maven方式)
  13. Linux中MySQL配置文件my.cnf参数优化
  14. Spring Cloud Alibaba基础教程:Nacos配置的多环境管理
  15. Extjs 解决grid分页bug问题
  16. 如何去掉wordpress后台notice提示窗口
  17. 1.golang的环境搭建及入门
  18. Python IPy模块
  19. System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---&gt; System.InvalidOperationException: 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
  20. VC播放mp3的方法

热门文章

  1. linux学习之高并发服务器篇(二)
  2. HTML学习----------DAY2第五节
  3. 洛谷 P1898 缘分计算
  4. iOS数字媒体开发浅析
  5. 一些牛人的IOS博客,mark下慢慢学习
  6. [2012山东省第三届ACM大学生程序设计竞赛]——Mine Number
  7. ES6第一节:开发环境的搭建
  8. Vijos——T1406 拉力赛
  9. myeclipse中断点调试
  10. python-openpyxl安装