Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中应用接口和抽象_Php_Oop_Interface - Fatal编程技术网

如何在PHP中应用接口和抽象

如何在PHP中应用接口和抽象,php,oop,interface,Php,Oop,Interface,所以我已经理解了接口和抽象在PHP中是如何工作的,例如,我不认为有一个接口的意义,如果它只是设置了一个指南,并且要求实现的对象具有某些方法。尤其是因为接口甚至没有被实例化 这也与抽象有关,我只是不能将它应用到我的代码中,并将其视为一件伟大的事情。当我试图在更大范围内创建对象以相互交互以确定接口时,每个类最终都会来回传递信息,但从来不会触及接口 所以我想问的是,你们是否有任何建议或链接到外部资源,能够很好地解释这类事情。我可以给你们举一个尽可能简单的例子 假设您想要一个允许您的站点使用Facebo

所以我已经理解了接口和抽象在PHP中是如何工作的,例如,我不认为有一个接口的意义,如果它只是设置了一个指南,并且要求实现的对象具有某些方法。尤其是因为接口甚至没有被实例化

这也与抽象有关,我只是不能将它应用到我的代码中,并将其视为一件伟大的事情。当我试图在更大范围内创建对象以相互交互以确定接口时,每个类最终都会来回传递信息,但从来不会触及接口


所以我想问的是,你们是否有任何建议或链接到外部资源,能够很好地解释这类事情。

我可以给你们举一个尽可能简单的例子

假设您想要一个允许您的站点使用Facebook/Twitter登录的功能

# here's your interface/abstract class
interface Auth_Adapter {
    public function auth();
}

# now your Facebook
class Auth_Adapter_Facebook implements Auth_Adapter {
    public function login() {
        # include facebook-sdk and auth
    }
}

# Twitter
class Auth_Adapter_Twitter implements Auth_Adapter {
    public function login() {
        # include twitter-oauth and auth
    }
}
想象一下,当有人尝试使用Facebook/Twitter时,他们只需拨打电话即可

$adapter = new Auth_Adapter_Facebook; 
$adapter->login();

$adapter = new Auth_Adapter_Twitter; 
$adapter->login();
正如您所看到的,两个适配器使用相同的
登录
界面。如果将来您必须包括“Pinterest”登录,会发生什么情况?只要实现相同的接口,代码仍然可以工作

编辑:更多说明

下面是您必须使用
接口或摘要的原因

# I use `type-hinting` here. So I can ensure that only object that implements `Auth_Adapter` will allow. Without this implementation someone might pass some other object that doesn't have `login` method in. But in our case we don't have to worry about that.
public function perform_login(Auth_Adapter $adapter) {

    $adapter->login();
}

这里有一个简单的例子。通过创建接口和抽象类,可以确保对象与公共API相关联。请参见下面的示例

interface iCar
{
    function drive();
}

abstract class Car implements iCar
{
    public $make = 'Generic';

    public function drive()
    {
        printf("I'm driving in my %s%s", $this->make, PHP_EOL);
    }
}

class FordTruck extends Car
{
    public $make = "Ford";
}

class Porsche extends Car
{
    public $make = 'Porsche';
    public function drive()
    {
        printf("I'm speeding around in my %s%s", $this->make, PHP_EOL);
    }
}

class Yugo extends Car
{
    public $make = 'Yugo';

    public function drive()
    {
        printf("I'm pushing my %s around town%s", $this->make, PHP_EOL);
    }
}

function drive(iCar $car)
{
    $car->drive();
}

$car1 = new FordTruck;
$car2 = new Porsche;
$car3 = new Yugo;


drive($car1);
drive($car2);
drive($car3);
即使没有在
drive()
函数上指定输入参数的类型,也可以检查输入是否是iCar的
实例

function drive($car)
{
    if ($car instanceof iCar)
        $car->drive();
}

另一个例子是在应用程序中构建缓存接口。您可以指定所有缓存引擎都支持相同的方法来读取/写入/使缓存中的对象无效,而不知道(或关心)特定缓存引擎的实际实现。

例如,使用“Zend framework”之类的框架,尝试了解其一个组件的工作原理,你会得到真实世界的例子。我理解这一点,但是你不能拿出
实现身份验证适配器
,它会做完全相同的事情吗?如果你简单地使用
类Car实现iCar
而不是
抽象类Car实现iCar
,会发生什么?