请解释一下什么时候我应该使用PHP接口,什么时候我应该使用抽象类?

我如何能改变我的抽象类在一个接口?


当前回答

从哲学的角度来看:

An abstract class represents an "is a" relationship. Lets say I have fruits, well I would have a Fruit abstract class that shares common responsabilities and common behavior. An interface represents a "should do" relationship. An interface, in my opinion (which is the opinion of a junior dev), should be named by an action, or something close to an action, (Sorry, can't find the word, I'm not an english native speaker) lets say IEatable. You know it can be eaten, but you don't know what you eat.

从编码的角度来看:

如果您的对象有重复的代码,这表明它们有共同的行为,这意味着您可能需要一个抽象类来重用代码,而这是不能用接口实现的。 另一个区别是,一个对象可以实现任意多的接口,但是由于“菱形问题”,您只能有一个抽象类(查看这里了解原因!http://en.wikipedia.org/wiki/Multiple_inheritance The_diamond_problem)

我可能忘记了一些要点,但我希望它能澄清一些事情。

PS:“是一个”/“应该做”是由Vivek Vermani的答案带来的,我不是故意偷他的答案,只是重复使用这些术语,因为我喜欢它们!

其他回答

主要的区别是抽象类可以包含默认实现,而接口不能。

接口是没有任何实现的行为契约。

最佳实践是使用接口来指定契约,并将抽象类作为契约的一个实现。该抽象类可以填充大量样板文件,因此您可以通过重写需要或想要的内容来创建实现,而不必强制使用特定的实现。

从哲学的角度来看:

An abstract class represents an "is a" relationship. Lets say I have fruits, well I would have a Fruit abstract class that shares common responsabilities and common behavior. An interface represents a "should do" relationship. An interface, in my opinion (which is the opinion of a junior dev), should be named by an action, or something close to an action, (Sorry, can't find the word, I'm not an english native speaker) lets say IEatable. You know it can be eaten, but you don't know what you eat.

从编码的角度来看:

如果您的对象有重复的代码,这表明它们有共同的行为,这意味着您可能需要一个抽象类来重用代码,而这是不能用接口实现的。 另一个区别是,一个对象可以实现任意多的接口,但是由于“菱形问题”,您只能有一个抽象类(查看这里了解原因!http://en.wikipedia.org/wiki/Multiple_inheritance The_diamond_problem)

我可能忘记了一些要点,但我希望它能澄清一些事情。

PS:“是一个”/“应该做”是由Vivek Vermani的答案带来的,我不是故意偷他的答案,只是重复使用这些术语,因为我喜欢它们!

为什么要使用抽象类?下面是一个简单的例子。假设我们有以下代码:

<?php 

class Fruit {
    private $color;

    public function eat() {
        // chew
    }

    public function setColor($c) {
        $this->color = $c;
    }
}

class Apple extends Fruit {
    public function eat() {
        // chew until core
    }
}

class Orange extends Fruit {
    public function eat() {
        // peeling
        // chew
    }
}

现在我给你一个苹果,你吃了它。 它尝起来像什么?它尝起来像苹果。

<?php 
$apple = new Apple();
$apple->eat();

// Now I give you a fruit.
$fruit = new Fruit();
$fruit->eat();

那是什么味道?这没什么意义,所以你不能这么做。这是通过使Fruit类抽象以及其中的eat方法来实现的。

<?php 
abstract class Fruit {
    private $color;

    abstract public function eat(){}

    public function setColor($c) {
        $this->color = $c;
    }
}
?>

一个抽象类就像一个接口,但是你可以在一个抽象类中定义方法,而在一个接口中它们都是抽象的。抽象类可以有空方法和工作/具体方法。在接口中,定义的函数不能有主体。在抽象类中,它们可以。

一个真实的例子:

<?php 
abstract class person {

    public $LastName;
    public $FirstName;
    public $BirthDate;

    abstract protected function write_info();
}

final class employee extends person{

    public $EmployeeNumber;
    public $DateHired;

    public function write_info(){
        //sql codes here
        echo "Writing ". $this->LastName . "'s info to emloyee dbase table <br>";   
    }
}

final class student extends person{

    public $StudentNumber;
    public $CourseName;

    public function write_info(){
        //sql codes here
        echo "Writing ". $this->LastName . "'s info to student dbase table <br>";
    }
}

///----------
$personA = new employee;
$personB = new student;

$personA->FirstName="Joe";
$personA->LastName="Sbody";

$personB->FirstName="Ben";
$personB->LastName="Dover";

$personA->write_info();
// Writing Sbody's info to emloyee dbase table
$personB->write_info();
// Writing Dover's info to student dbase table 

抽象类和接口之间的技术差异已经在其他答案中列出了。为了面向对象编程,我想在编写代码时对类和接口之间的选择添加一个解释。

类应该代表实体,而接口应该代表行为。

让我们举个例子。计算机显示器是一个实体,应该表示为一个类。

class Monitor{
    private int monitorNo;
}

它的设计目的是为您提供一个显示界面,因此功能应该由接口定义。

interface Display{
    void display();
}

正如其他答案中解释的那样,还有许多其他的事情需要考虑,但这是大多数人在编码时忽略的最基本的事情。