【问题标题】:What is the difference between abstract and interface in php? [duplicate]php中的抽象和接口有什么区别? [复制]
【发布时间】:2011-04-04 08:59:57
【问题描述】:

可能重复:
PHP: What is the difference between an interface and abstract class?

据我了解,实现或扩展抽象或接口类的类必须使用默认方法。我知道我们可以使用 implement 关键字来使用多个接口,但我们只能扩展 1 个抽象。在现实生活项目中使用哪一个和区别?

【问题讨论】:

标签: php class oop interface abstract-class


【解决方案1】:

这些差异既有理论性的,也有实践性的:

  • interface 是对您的类拥有和宣传的一些能力的描述(因此实现相同接口的各种类可以以相同的方式使用)
  • 抽象类可以是一个默认实现,包含可能出现在所有实现中的部分。它不必实现完整的接口

示例 - 一个接口:

// define what any class implementing this must be capable of
interface IRetrieveData {
    // retrieve the resource
    function fetch($url);

    // get the result of the retrieval (true on success, false otherwise)
    function getOperationResult();

    // what is this class called?
    function getMyClassName();
}

现在我们有了一组要求,这些要求将针对每个实现它的类进行检查。让我们创建一个抽象类及其子类:

// define default behavior for the children of this class
abstract class AbstractRetriever implements IRetrieveData {
    protected $result = false;

    // define here, so we don't need to define this in every implementation
    function getResult() {
       return $result;
    }

    // note we're not implementing the other two methods,
    // as this will be very different for each class.
}

class CurlRetriever extends AbstractRetriever {
     function fetch($url) {
         // (setup, config etc...)
         $out = curl_execute();
         $this->result = !(curl_error());
         return $out;
     }
     function getMyClassName() {
         return 'CurlRetriever is my name!';
     }
}

class PhpRetriever extends AbstractRetriever {
     function fetch($url) {
        $out = file_get_contents($url);
        $this->result = ($out !== FALSE);
        return $out;
     }
     function getMyClassName() {
         return 'PhpRetriever';
     }
}

一个完全不同的抽象类(与接口无关),有一个实现我们接口的子类:

abstract class AbstractDog {
     function bark() {
         return 'Woof!'; 
     }
}

class GoldenRetriever extends AbstractDog implements IRetrieveData {
     // this class has a completely different implementation
     // than AbstractRetriever
     // so it doesn't make sense to extend AbstractRetriever
     // however, we need to implement all the methods of the interface
     private $hasFetched = false;

     function getResult() {
         return $this->hasFetched;
     }

     function fetch($url) {
         // (some retrieval code etc...)
         $this->hasFetched = true;
         return $response;
     }
     function getMyClassName() {
         return parent::bark();
     }
}

现在,在其他代码中,我们可以这样做:

function getStuff(IRetrieveData $retriever, $url) {
    $stuff = $retriever->fetch($url);
}

而且我们不必担心将传入哪些检索器(cURL、PHP 或 Golden),以及它们将如何实现目标,因为所有检索器都应该具有相似的行为。你也可以使用抽象类来做到这一点,但是你会根据类的祖先而不是它的能力来限制自己。

【讨论】:

  • Ty 为例子....+1
【解决方案2】:

多重继承与单一继承:

  • 您只能从单个抽象类继承
  • 您可以实现多个接口

实施:

  • 抽象类中实际上可以包含功能代码。这使您可以在子类之间共享实现
  • 接口只定义公共成员函数。实现相同接口的类实际上并不共享代码。

这就是我不经意间知道的。

【讨论】:

    【解决方案3】:

    我听到最好的比喻是抽象类是一个半完成的类。它没有完成;你仍然必须完成它。所以当你创建一个扩展抽象类的类时,你只是完成了你在抽象类中开始的事情。这也是为什么不能实例化抽象类的原因;你把它抽象化表明它是不完整的。它仍然需要一些额外的功能。

    an 接口只是保证某些方法,每个方法都有一定数量的参数,必须存在于实现它的类中。这样以后,使用实现特定接口的类的程序员可以放心,他们可以调用该类的某些方法。

    【讨论】:

      【解决方案4】:
      【解决方案5】:

      这里很好地描述了两者之间的区别:

      http://www.supertom.com/code/php_abstracts_and_interfaces.html

      归根结底,extends 是“is-a”关系,implements 是“has-a”关系。

      【讨论】:

        【解决方案6】:
        "An Abstract Class can contain default Implementation, where as an 
        Interface should not contain any implementation at all. "
        

        至于在现实世界的应用程序中使用哪个......它真的归结为上下文。

        例如,有一个question on here the other day about implementing a game using PHP。在这里,他们有一个定义怪物的抽象类,任何怪物都可以基于这个抽象类。这允许继承默认怪物属性。

        而对于接口,您正在为“接口”(请原谅使用解释中的术语)某些系统的方式定义一般要求。我最近做的一个项目中的一个例子。我在 php 中实现了一个soapclient 来与第三方的soapserver 进行交互。这个接口定义了服务器支持的soap方法,因此任何实现我的接口的类都必须定义这些方法。

        【讨论】:

          猜你喜欢
          • 2010-11-13
          • 2013-02-17
          • 2019-03-02
          • 2012-04-28
          • 2019-12-14
          • 2019-10-17
          • 2010-12-27
          相关资源
          最近更新 更多