欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP OPP机制和模式简介(抽象类、接口和契约式编程)

程序员文章站 2024-02-12 23:03:34
...

本文将介绍抽象类、接口和一种称为契约式编程的技术。使用这些OPP机制,所编写的代码就不限于只能计算或者输出内容了。这些机制能够在概念层次上定义类之间交互

1.抽象类

抽象类机制中总是要定义一个公共的基类,而将特定的细节留给继承者来实现。通过抽象概念,可以在开发项目中创建扩展性很好的架构。任何一个类,如果它里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象的。被定义为抽象的方法只是声明了其调用方式(参数),不能定义其具体的功能实现。在类的声明中使用 abstract 修饰符就可以将某个类声明为抽象的。

1.1方法原型(prototype)

是指方法的定义中剔除了方法体之后的签名。它包括存取级别、函数关键字、函数名称和参数。他不包含({})或者括号内部的任何代码。例如下面的代码就是一个方法原型:

复制代码 代码如下:


public function prototypeName($protoParam)

继承一个抽象类的时候,子类必须定义父类中的所有抽象方法;另外,这些方法的访问控制必须和父类中一样(或者更为宽松)。

1.2关于抽象类

某个类只要包含至少一个抽象方法就必须声明为抽象类
声明为抽象的方法,在实现的时候必须包含相同的或者更低的访问级别。
不能使用 new 关键字创建抽象类的实例。
被生命为抽象的方法不能包含函数体。
如果将扩展的类也声明为抽象类,在扩展抽象类时,可以不用实现所有的抽象方法。(如果某个类从抽象类继承,当它没有实现基类中所声明的所有抽象方法时,它就必须也被声明为抽象的。)

1.3使用抽象类

复制代码 代码如下:


abstract class Car
{
abstract function getMaxSpeend();
}

class Roadster extends Car
{
public $Speend;

public function SetSpeend($speend = 0)
{
$this->Speend = $speend;
}
public function getMaxSpeend()
{
return $this->Speend;
}
}

class Street
{
public $Cars ;
public $SpeendLimit ;

function __construct( $speendLimit = 200)
{
$this -> SpeendLimit = $speendLimit;
$this -> Cars = array();
}

protected function IsStreetLegal($car)
{
if ($car->getMaxSpeend() SpeendLimit)
{
return true;
}
else
{
return false;
}
}

public function AddCar($car)
{
if($this->IsStreetLegal($car))
{
echo 'The Car was allowed on the road.';
$this->Cars[] = $car;
}
else
{
echo 'The Car is too fast and was not allowed on the road.';
}
}
}

$Porsche911 = new Roadster();
$Porsche911->SetSpeend(340);

$FuWaiStreet = new Street(80);
$FuWaiStreet->AddCar($Porsche911);

/**
*
* @result
*
* The Car is too fast and was not allowed on the road.[Finished in 0.1s]
*
*/
?>

2.对象接口

使用接口(interface),可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容。

接口是通过 interface 关键字来定义的,就像定义一个标准的类一样,但其中定义所有的方法都是空的。

接口中定义的所有方法都必须是公有,这是接口的特性。

接口是一种类似于类的结构,可用于声明实现类所必须声明的方法。例如,接口通常用来声明API,而不用定义如何实现这个API。

大多数开发人员选择在接口名称前加上大写字母I作为前缀,以便在代码和生成的文档中将其与类区别开来。

2.1使用接口

和集成抽象类需要使用 extends 关键字不同的是,实现接口使用的是 implements 关键字。一个类可以实现多个接口,这时,我们需要用逗号将他们隔开。如果将某个类标记为实现了某个接口,但却没有实现这个借口的所有方法,将会抛出错误。

2.2使用接口的案例

复制代码 代码如下:


abstract class Car
{
abstract function SetSpeend($speend = 0);
}

interface ISpeendInfo
{
function GetMaxSpeend();
}

class Roadster extends Car implements ISpeendInfo
{

public $Speend;

public function SetSpeend($speend = 0)
{
$this->Speend = $speend;
}

public function getMaxSpeend()
{
return $this->Speend;
}
}

class Street
{

public $Cars ;
public $SpeendLimit ;

function __construct( $speendLimit = 200)
{
$this -> SpeendLimit = $speendLimit;
$this -> Cars = array();
}

protected function IsStreetLegal($car)
{
if ($car->getMaxSpeend() SpeendLimit)
{
return true;
}
else
{
return false;
}
}

public function AddCar($car)
{
if($this->IsStreetLegal($car))
{
echo 'The Car was allowed on the road.';
$this->Cars[] = $car;
}
else
{
echo 'The Car is too fast and was not allowed on the road.';
}
}
}


$Porsche911 = new Roadster();
$Porsche911->SetSpeend(340);

$FuWaiStreet = new Street(80);
$FuWaiStreet->AddCar($Porsche911);

/**
*
* @result
*
* The Car is too fast and was not allowed on the road.[Finished in 0.1s]
*
*/
?>

3.instanceof 操作符

instanceof 操作符是PHP5中的一个比较操作符。他接受左右两边的参数,并返回一个boolean值。这个操作符是用来确定对象的某个实例是否为特定的类型,或者是否从某个类型继承,又或者实现类某个特定的接口。

复制代码 代码如下: