本章将介绍php类.
现在,基本上每种语言都支持面向对象,当然php也不列外。
php在以前的版本不支持面向对象,但自从php4,包括php4之后开始支持了。本系列将以php5为例来描述面向对象的知识。在后面也将提及php4的知识。
php对面向对象的支持的能力,以我个人观点,没有c++,c#,java等语言那么深入,即使如此,也算得上是比较全面的。
你问问现在的每一个人面向对象的特点是什么?他们肯定回答封装,多态,继承,没错,就是这些。
类
好了,我们从类开始说起吧。类的概念不说了,世人都知道。
php中的类可以这样写:
php
class page
{
}
?>
它定义了一个简单的类page,什么都没有。
你也可以定义属性和方法,如下:
php
class page
{
// 2 attributes
var $attribute1 ;
var $attribute2 ;
// 2 functions
function operation1()
{
}
function operation2( $param1 , $param2 )
{
}
}
?>
php的类有构造和析构函数吗?答案是肯定的,有!那么是不是和c++的一样呢?不是,它有专门的函数。
构造用__construct();析构用__destruct();( 注意:前面是2个下划线)
代码如下: 1 php
2 class page
3 {
4 // 2 attributes
5 var $attribute1 ;
6 var $attribute2 ;
7
8 // 2 functions
9 function operation1()
10 {
11 }
12
13 function operation2( $param1 , $param2 )
14 {
15 }
16
17 // construction function
18 function __construct( $param )
19 {
20 $this -> attribute1 = $param ;
21 }
22
23 // destruction function
24 function __destruct()
25 {
26 $this -> attribute1 = 0 ;
27 }
28 }
29 ?>
php支持重载吗? 是的,php支持重载,这样就可以有多种方式进行构造了。
类的实例化
构建了类,我们就需要应用它,如何使用?实例化,这个和其他语言一样,所以只给出代码:
1 php
2 class page
3 {
4 // 2 attributes
5 var $attribute1 ;
6 var $attribute2 ;
7
8 // 2 functions
9 function operation1()
10 {
11 }
12
13 function operation2( $param1 , $param2 )
14 {
15 }
16
17 // construction function
18 function __construct( $param )
19 {
20 $this -> attribute1 = $param ;
21 echo it will construct the attribute: {$this->attribute1}
;
22 }
23
24 // destruction function
25 function __destruct()
26 {
27 $this -> attribute1 = 0 ;
28 }
29 }
30 $instance1 = new page( attribute1 in instance 1 );
31 $instance2 = new page( attribute2 in instance 2 );
32 $instance1 = new page();
33 ?>
输出结果是:
1 it will construct the attribute : attribute1 in instance 1
2 it will construct the attribute : attribute2 in instance 2
3 it will construct the attribute :
设置或得到属性
这个问题很有意思。
拿上面的例子来说, $attribute1 就是一个属性,我们可以利用 $this -> attribute1 来获取或设置其值。
但是为了更好的体现类的封装性,还是采用php提供的get或set方法,这个和c#里的功能这样。但是在php中,提供2个方法:__get($name),__set($name,$value)函数来设置属性。
这样做的另外一个好处就是根据实际需要,加入自己的逻辑,如果直接赋值的话就不能办到。比如说属性一定要是1-100之间的数,怎么办?还是采用__set吧。看看代码吧:
1 attribute1=$param;
21 echo it will construct the attribute: {$this->attribute1}
;
22 }
23
24 //destruction function
25 function __destruct()
26 {
27 $this->attribute1=0;
28 }
29 function __set($name,$value)
30 {
31 if($name===attribute2&&$value>=2&&attribute232 {
33 $this->attribute2=$value;
34 }
35 }
36 function __get($name)
37 {
38 return $this->$name;
39 }
40 }
41 $instance1=new page(attribute1 in instance 1);
42 $instance2=new page(attribute1 in instance 1);
43 $instance3=new page();
44 $instance3->attribute2=99;
45 echo $instance3->attribute2;
46 ?>
类成员和方法的可视性
还是public,private以及protected。和c#的作用一样。但是需要注意的是,php默认的是public,而不是大多数语言默认的private。基于此,上述的例子才可以使用。还需要注意的一点是,如果使用了这3个关键字,var就省略了。看个例子吧。
1 attribute1=$param;
21 echo it will construct the attribute: {$this->attribute1}
;
22 }
23
24 //destruction function
25 public function __destruct()
26 {
27 $this->attribute1=0;
28 }
29 public function __set($name,$value)
30 {
31 if($name===attribute2&&$value>=2&&attribute232 {
33 $this->attribute2=$value;
34 }
35 }
36 public function __get($name)
37 {
38 return $this->$name;
39 }
40 }
41 $instance1=new page(attribute1 in instance 1);
42 $instance2=new page(attribute1 in instance 1);
43 $instance3=new page();
44 $instance3->attribute2=99;
45 echo $instance3->attribute2;
46 ?>
类的继承
类的继承和c#,java语言一样,不支持多重继承。php实用extends来继承一个类,和java有些类似。
在java里有final这个词,php里也有,并且作用相似,就是为了阻止继承或重载,重写方法。
提到继承,那么php支持接口吗?支持。例如:
1 php
2 interface idispose
3 {
4 function dispose();
5 }
6
7 class page extends idispose
8 {
9 function dispose()
10 {
11 // your code here.
12 }
13 }
14 ?>
php新的高级功能
1. per-class常量
它是在php5引入的。这个常理不需要再初始化。例如:
1
其实和c++里的静态变量一样。
2. 静态函数
既然说到了静态变量,当然也要说静态函数了。它和上面的思想一样,实现时需要加static关键字,好多语言都是这样的。c#,java等。
代码可以如下:
1
3. php 的类的类型检查
你要想判断一个类是不是某个类的类型。非常简单,就是instanceof,如果你使用过java,那么你非常熟悉了,因为和java一样。如果你是用c#的,类似于c# 里的is。
4. 克隆对象
php提供一个关键字,clone,就是这个功能。我看php是我见到语言中克隆对象比较简单的语言之一。代码如下:
$c = clone $b
如果你想改变$c的某一特性,怎么办?这个时候你需要重写__clone方法。
5. 抽象类
php也支持抽象类,并且和c#等语言非常类似。给个例子吧。
1 getsquared()}
;
9 echo and the number of lines is :{$this->getlinecount()}
;
10 }
11 }
12
13 class rectange extends shape
14 {
15 protected function getsquared()
16 {
17 return 12*12;
18 }
19 protected function getlinecount()
20 {
21 return 4;
22 }
23 }
24
25 class triangle extends shape
26 {
27 protected function getsquared()
28 {
29 return 10*12;
30 }
31 protected function getlinecount()
32 {
33 return 3;
34 }
35 }
36
37 $rect=new rectange();
38 $rect->printshapevalue();
39
40 $tri=new triangle();
41 $tri->printshapevalue();
42 ?>
输出的结果如下:
the squared size is : 144
and the number of lines is : 4
the squared size is : 120
and the number of lines is : 3
注意抽象函数的写法,abstract关键字。以及在子类里,没有override关键字,而是直接重写。这是和c#不同的。
6. 使用__call重载
这是我认为php提供的一个非常cool的功能,非常的佩服。这个方法用来监视一个对象中的其它方法。如果你试着调用一个对象中不存在的方法,__call 方法将会被自动调用。
举个例子吧。代码如下:
1 x;
11 }
12 }
13
14 $foo = new caller();
15 $a = $foo->test1(1, 2, 3.4, true);
16 var_dump($a);
17 ?>
显示的结果如下:
method test1 called : array ( 4 ) { [ 0 ] => int( 1 ) [ 1 ] => string ( 1 ) 2 [ 2 ] => float ( 3.4 ) [ 3 ] => bool( true ) } array ( 3 ) { [ 0 ] => int( 1 ) [ 1 ] => int( 2 ) [ 2 ] => int( 3 ) }
$foo->test1这个方法在foo类里不存在的,但是定义了__call之后,就可以了。
7. 使用__autoload()方法。
这个功能仍然很cool。很多开发者写面向象的应用程序时对对每个类的定义建立一个 php 源文件。一个很大的痛苦是不得不在每个脚本(每个类一个文件)开头写一个长长的包含文件列表。在 php 5 中,我们再也不需要这样了。 __autoload 函数就能轻松搞定,当代码视图使用未定义的类时,此方法自动调用。通过调用此函数,脚本引擎在出错失败之前就有了最后一个补救的机会来加载所需的类。但是需要注意的是 __autoload 函数抛出的异常不能被 catch 语句块捕获并导致致命错误。
一个例子:
1 php
2 function __autoload( $class_name ) {
3 require_once $class_name . ' .php ' ;
4 }
5
6 $obj1 = new myclass1();
7 $obj2 = new myclass2();
8 ?>
8,实现迭代期或迭代。
它将迭代类的所有可见的属性以及值等
9 . 将类型转化为字符串(__tostring()).
这是一个魔术函数的应用,__tostring().它有点像c#里的tostring()函数一样,但是对于类本身来说还是有区别的。如果你在php的类里实现了该函数,它们载打印这个类时就会调用此函数,例如:
1 php
2 class page
3 {
4 public function __tostring()
5 {
6 return ' hello ' ;
7 }
8 }
9 $page = new page();
10 echo $page ;
11 ?>
它将输出:
hello
10. 反射。
没想到php也支持反射的。如果你对c#的反射熟悉的话,它们我就不用说了。如果不是,请继续往下看。
反射是通过类或对象查找它包含的内容的信息的能力。当此类的文档信息不详或没有提供时,可以通过反射得到类的一些属性,方法等信息。
反射继承了zend的一些类。具体实用请参看php反射的类的文档。
