background image

private

 

$height

function

 __construct(

$width

=0, 

$height

=0){ 

  

$this

->width=

$width

  

$this

->height=

$height

function

 get_area(){ 

  

echo

 (

$this

->width+

$this

->height)*2; 


$Shape_Rect

 = 

new

 Rectangle(20,30); 

$Shape_Rect

->get_area(); 

?> 
 
这也算是一个简单的例子,基本上可以说明

PHP 中抽象类的用法了,其他的不想多说了。

个人觉得抽象类一般在大的项目才会用吧,因为我觉得它实在是要遵守的

”规矩”太多了,

用起来不方便!当然这只是我的意见。还想多说一些,

PHP 抽象类应用是单继承的,也就是

说你只能从一个类继承,而不能一个类继承了

A 类,又继承 B 类,如果要实现这样的功能,

得使用接口相关的知识了,在此暂时不讨论

PHP 接口的知识!一句话:单继承多接口中!