English 中文(简体)
如何在 PHP 中应用界面和抽象化
原标题:How to apply interfaces and abstraction in PHP

所以我理解了 PHP 的界面和抽象化是如何工作的, 我只是不明白,比如说, 如果它只设置一个指南, 并且要求被执行的对象拥有某些方法, 那么它是否拥有一个界面, 特别是因为接口甚至没有被即时化。

这也带有抽象性, 我只是不能把它应用到我的代码中, 把它看成是一件伟大的事情。 当我试图在更大的规模上创建物体来相互交流, 以便找出接口时, 每一类最终都会不断传递信息, 但它从未被触碰过。

所以我要问的是 你们是否与外界人士 有任何建议或联系 能很好地解释这种事情

最佳回答

这里请举一个简单的例子。创建界面和抽象类可以确保对象对普通 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 () 函数上指定输入参数的类型, 您也可以检查输入是否是 instanceof a icar

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

另一个例子是在您的应用程序中建立一个缓存界面。 您可以指定所有缓存引擎都支持在缓存中读/ 写/ 校验对象的相同方法, 而不知道( 或关心) 某缓存引擎的实际实施情况 。

问题回答

我可以给你一个最简单的例子

假设您想要一个功能, 允许您的网站与 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();

您可以看到两个适配器使用相同的 < code> login 界面。 如果将来您必须包含 Pinterest 登录, 会发生什么? 只要您执行相同的界面, 您的代码仍然有效 。

<强度 > EDIT: 更多解释

在这里您必须使用 界面或抽象 的原因

# 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();
}




相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签