Notice: This article is working on progress!
本章将介绍PHP类.
现在,基本上每种语言都支持面向对象,当然PHP也不列外。
PHP在以前的版本不支持面向对象,但自从PHP4,包括PHP4之后开始支持了。本系列将以PHP5为例来描述面向对象的知识。在后面也将提及PHP4的知识。
PHP对面向对象的支持的能力,以我个人观点,没有C++,C#,Java等语言那么深入,即使如此,也算得上是比较全面的。
你问问现在的每一个人面向对象的特点是什么?他们肯定回答封装,多态,继承,没错,就是这些。
类
好了,我们从类开始说起吧。类的概念不说了,世人都知道。
PHP中的类可以这样写:
php
class Page
{
}
?>
php
class Page
{
// 2 attributes
var $attribute1 ;
var $attribute2 ;
// 2 functions
function Operation1()
{
}
function Operation2( $param1 , $param2 )
{
}
}
?>
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 ?>
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 :
1 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 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 ?>
1 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}
";
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 ?>
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 ?>
1 2 class Math
3 {
4 const pi=3.1415927;
5 }
6 echo "Math::pi='.Math::pi."\n";
7 ?>
1 2 class Shape
3 {
4 static function SquredSize($length)
5 {
6 return $length*$length;
7 }
8 }
9 echo "Squre:".Shape::SquredSize(10);
10 ?>
$c = clone $b
如果你想改变$c的某一特性,怎么办?这个时候你需要重写__clone方法。
1 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()}
";
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
1 2 class Caller
3 {
4 private $x = array(1, 2, 3);
5
6 public function __call($m, $a)
7 {
8 print "Method $m called:\n";
9 var_dump($a);
10 return $this->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之后,就可以了。 1 php
2 function __autoload( $class_name ) {
3 require_once $class_name . ' .php ' ;
4 }
5
6 $obj1 = new MyClass1();
7 $obj2 = new MyClass2();
8 ?>
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. 反射。