All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET中利用VWD操作数据库_[Asp.Net教程]
用XML和XSL来生成动态页面_[XML教程]
PHP5.2+APACHE2.2+BugFree1.0的安装_[PHP教程]
MYSQL中如何设列的默认值为Now()_mssql学习_编程技术
Karrigell,入门_php资料_编程技术
SQL Server 索引基础知识(1)--- 记录数据的基本格式_[SQL Server教程]
GridView绑定模板格式化日期总结_[Asp.Net教程]
ASP.NET,2.0,Membership_[Asp.Net教程]
delphi滚动条组件(TScrollBar)使用方法
全面接触SQL语法(4)_mssql学习_编程技术
Reading number is top 10 pictures
So beauty, will let you spray blood7
2012 national geographic daily picture9
一个武林高手的故事
The household design, do not do bridal chamber a pity
美女挤公交
云南大理的美女
身材野火台灣美女1
俄罗斯台球天后惊艳魅惑1
美奂绝伦的风景
西游日记4
Download software ranking
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
Adobe Flash Player(IE) 10.0.32.18 浏览器专用的FLASH插件
C++编程教程第三版
Tram sex maniac 2 (H) rar bag6
仙剑奇侠传98版歌曲
Sora aoi, the maid, students' uniforms
都市狐狸姑娘传
Unix video tutorial3
C#程序员参考手册
matrix2
归海一刀 published in(发表于) 2014/2/10 6:46:07 Edit(编辑)
PHP5中的this,self和parent关键字详解_[PHP教程]

PHP5中的this,self和parent关键字详解_[PHP教程]

PHP5中的this,self和parent关键字详解_[PHP教程]

PHP5是一具备了大部分面向对象语言的特性的语言,比PHP4有了很多的面向对象的特性,但是有部分概念也比较绕人,所以今天拿出来说说,说的不好,请高手见谅. (阅读本文,需要了解PHP5的面向对象的知识)

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


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


  (1) this


1 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 ),那么当然就输出了"heiyeluren"。第二个实例的时候,print( this->name )变成了print( nameObject2->name ),于是就输出了"PHP5"。所以说,this就是指向当前对象实例的指针,不指向任何其他对象或类。


  (2)self


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


1 2
3 class Counter
4 {
5 //定义属性,包括一个静态变量
6 private static firstCount = 0;
7 private lastCount;
8
9 //构造函数
10 function __construct()
11 {
12 this->lastCount = ++selft::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 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是对父类的引用。基本上我所了解就这么多,肯定有理解错误之处,请高手指出!


  我的邮箱: heiyeluren@163.com WriteTime: 2004-11-3 18:30


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.