<?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}<br>";
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 <?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}<br>";
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&&attribute2<=100)
32 {
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 <?php
2 class Page
3 {
4 //2 attributes
5 public $attribute1;
6 public $attribute2;
7
8 //2 functions
9 public function Operation1()
10 {
11 }
12
13 public function Operation2($param1,$param2)
14 {
15 }
16
17 //construction function
18 public function __construct($param)
19 {
20 $this->attribute1=$param;
21 echo "It will construct the attribute: {$this->attribute1}<br>";
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&&attribute2<=100)
32 {
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 abstract class Shape
3 {
4 abstract protected function getSquared();
5 abstract protected function getLineCount();
6 public function printShapeValue()
7 {
8 echo "The squared size is :{$this->getSquared()}<br>";
9 echo "And the number of lines is :{$this->getLineCount()}<br>";
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 方法将会被自动调用。
举个例子吧。代码如下: