PHP中的Abstract Class和Interface

简介:

原文:http://www.techflirt.com/tutorials/oop-in-php/abstract-classes-interface.html

最近开始学习PHP+MySQL,记录下学习过程中的重点内容吧,然后考虑把开发网站的过程也写一个系列Blog。

这篇blog主要介绍了Abstract ClassInterface的区别。

Abstract Class

什么是Abstract Class( 抽象类)

和C++中的抽象类概念一样,包含有纯虚函数(Java和Php中叫abstract method)的类叫做Abstract Class。 我们有时候也把abstract Class叫做base class,因为base class不能直接生成对象。

PHP 中的abstract Class

我们来看代码:

abstract class abc
{
public function xyz()
{
return 1;
}
}
$a = new abc();//this will throw error in php


PHP中的abstract class和其他oop语言一样,我们用关键字 abstract 来声明一个抽象类,如果你想直接生成这个类的对象,会报错。


abstract class testParent
{
public function abc()
{
//body of your funciton
}
}
class testChild extends testParent
{
public function xyz()
{
//body of your function
}
}
$a = new testChild();


testChild通过关键字extends 继承了抽象类 testParent, 然后我们就可以生成 一个testChild的对象了。

实现Abstract Method

类似于C++中的纯虚函数,我们只能在抽象类中申明Abstract method,而只能而且必须在子类中定义它。

其实这种说法不是绝对的,但是为了方便记忆,大多说的教材这样说,来我们回顾一下Effective C++中的关于pure virtual函数的讲解。

“Pure Virtual函数必须在derived class中重新声明,但是它们也可以有自己的实现”

class Airplane{
public:
    virtual void fly(const Airport& destination) = 0;
    ....
};

void Airplane::fly(const Airport& destination){
    // 缺省行为,将飞机飞到指定的目的地
}
class ModelA: public Airplane{
public:
    virtual void fly(const Airport& destination)
    {Airplane::fly(destination);}
    ....
};

class ModelB: public Airplane{
public: 
    virtual void fly(const Airport& destination);
    ....
};
void ModelB:: fly(const Airport& destination){
    // 将C型飞机飞到指定的地方
}
实际上,我们在derived class  ModelA中对virtual method做了一个inline调用

想在的fly被分割为两个基本要素:

其声明部分表现的是接口(这个derived class必须使用的)

其定义部分则变现出缺省行为(那是derived class可能使用的,但只有在它们明确提出申请时才是)


以上内容摘自《Effective C++改善程序与设计的55个具体做法 》条款34: 区分接口继承和实现继承


让我们回来继续讨论PHP中的abstract method的实现。

abstract class abc
{
abstract protected function f1($a , $b);
}
class xyz extends abc
{
protected function f1($name , $address)
{
echo "$name , $address";
}
}
$a = new xyz();

在abc中,我们用关键字abstract 声明了一个abstract method f1。在PHP中

一旦你在abstract class中声明了一个abstract method,那么所有继承这个class的subclass都必须要去declare这个method,否则,php会报错。


abstract class parentTest
{
abstract protected function f1();
abstract public function f2();
//abstract private function f3(); //this will trhow error
}
class childTest
{
public function f1()
{
//body of your function
}
public function f2()
{
//body of your function
}
protected function f3()
{
//body of your function
}
}
$a = new childTest();

上面的代码中可以看到,申明一个private的abstract method将会报错,因为private method只能在当前的类中使用。

注意到在abstract class中 f1函数是protected,但是在subclass中我们可以将其声明为public的。no any visibility is less restricted than public.

Interface

Interface in oop enforce definition of some set of method in the class。

interface将会强迫用户去实现一些method。例如有一个class中必须要求set ID和Name这两个属性,那么我们就可以把这个class申明为interface,这样所有继承自这个class的derived class都将强制必须实现setId和setName两个操作

Interface in php

Interface abc
{
public function xyz($b);
}

和其他oop语言一样,我们用关键字Interface 来声明。

在这个interface里面我们声明了一个method xyz,则任何时候,subclass中都必须申明这样的一个method xyz


class test implements abc
{
public function xyz($b)
{
//your function body
}
}

你可以用关键字 implements  来继承自interface。

在interface中,只能使用public,而不能使用诸如protected和private


interface template1
{
public function f1();
}
interface template2 extends template1
{
public function f2();
}
class abc implements template2
{
public function f1()
{
//Your function body
}
public function f2()
{
//your function body
}
}
你可以用extends关键字来继承interface,好像class那样。

这里的template2将包含所有template1的属性,因此在implements自template2的class  abc,将必须实现 function f1和f2,


你还可以extends multiple interface:

interface template1
{
public function f1();
}
interface template2
{
public function f2();
}
interface template3 extends template1, template2
{
public function f3();
}
class test implements template3
{
public function f1()
{
//your function body
}
public function f2()
{
//your function body
}
public function f3()
{
//your function body
}
}

同时,你的class也可以implements多个interface

interface template1
{
public function f1();
}
interface template2
{
public function f2();
}
class test implments template1, template2
{
public function f1()
{
//your function body
}
public function f2()
{
//your function body
}
}

但是如果两个interface包含同样名字的method,那么你的class将不能同时implement他们。


继承自interface中的method必须有同样的参数规范,例如下面的代码是可行的:

interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1($a)
{
echo $a;
}
}

但是这样的代码将会出错:

interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1()
{
echo $a;
}
}

但是,我们不需要把两个method里面的参数命名为同样的名称,下面的代码是可行的:

interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1($name)
{
echo $name;
}
}

同时,如果使用default value,你还可以改变参数的default value,下面的代码是可行的:

interface template1
{
public function f1($a = 20)
}
class test implements template1
{
public function f1($name  = "ankur")
{
echo $name;
}
}


Abstract Class和Interface之间的不同:

1. In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.

    在Abstract class中并非所有的method都必须是抽象的,但是在interface中所有的method都自动成为抽象的。就是在子类中必须声明和实现

2. Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance is possible in abstract classes.

    multiple和multilevel inheritance,我不知道改怎么翻译更好,multiple inheritance意思是 在interface中,一个class可以同时implements好多个interface;但是在abstract classes中,只能extends一个class。

   当然你extends的这个class可能又extentds别的class,这就是所谓的multilevel inheritance。

3. Method of php interface must be public only. Method in abstract class in php could be public or protected both.

    interface中的method必须是public的,但是在abstract class中可以是public或者protected。

4. In abstract class you can define as well as declare methods. But in interface you can only defined your methods.

    在abstract class中你可以同时声明(declare)和定义(define)methodes,但是在interface中你只能定义那个methods


目录
相关文章
|
4月前
|
PHP
PHP public、protected、private、static、abstract、final、interface、implements 区别对比
PHP public、protected、private、static、abstract、final、interface、implements 区别对比
29 0
|
4月前
|
PHP 数据安全/隐私保护
php中的abstract,interface,trait
1.抽象类 定义为抽象的类不能被实例化。任何一个类,如果它里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象的。被定义为抽象的方法只是声明了其调用方式(参数),不能定义其具体的功能实现。
29 0
|
PHP
PHP: Laravel报错Target class [App\\service\\AuthService] does not exist
PHP: Laravel报错Target class [App\\service\\AuthService] does not exist
96 0
|
网络协议 PHP
php实现多进程socket服务器class
php实现多进程socket服务器class
101 0
php实现多进程socket服务器class
|
PHP
【laralve】 执行php artisan db:sees Class GoodsAttributeTableSeeder does not exist
【laralve】 执行php artisan db:sees Class GoodsAttributeTableSeeder does not exist
76 0
【laralve】 执行php artisan db:sees Class GoodsAttributeTableSeeder does not exist
|
Java PHP Apache
PHP调用java的class
PHP调用java的class   转:http://hi.baidu.com/lei0827/blog/item/28439a4e923234ced1c86a18.html PHP调用java的class有2种方法     1、种是用php自身带的模块调用java(不过经过网友的证明发现这种方法不好)我没用。
1051 0
|
PHP
php调用webservice报错Class 'SoapClient' not found
原文: php调用webservice报错Class 'SoapClient' not found       php在调用webservice时,报告如下类似错误:            ( ! ) Fatal error: Class 'SoapCl...
1085 0