在PHP中创建一个接口,它可以指定一个类型来确定要使用的类 [英] Creating an interface in PHP, which can specify a type to determine which class to use

查看:278
本文介绍了在PHP中创建一个接口,它可以指定一个类型来确定要使用的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为PickupPoints创建一个界面,每个拾取点都需要能够返回找到的所有拾取点和拾取点详细信息,并且可能在将来提供更多信息。这可以使用下面的代码:

I'm creating an interface for "PickupPoints", Each pickup point needs to be able to return all pickup points found and the pickup point details and maybe in the future more information. That's okay with the code below:

<?php

interface iPickupPoint
{
    public function getPickupPoints($countryCode, $postalCode, $city);
    public function getPickupPointDetails($pickupPointId);
}


class PickupPoint1 implements iPickupPoint{
    ...
}

class PickupPoint2 implements iPickupPoint{
    ...
}

问题是我不想打电话给PickupPoint1,PickupPoint2,..他们自己的类。
我想要一个像PickupPoint(PickupPointType)这样的类,所以我只想给出拾取点的类型,类型需要是PickupPoint1,PickupPOint2,..

The problem is that I don't want to call the classes of PickupPoint1, PickupPoint2, .. them selves. I want a class like PickupPoint(PickupPointType) so I just want to give the type of pickup point and the types need to be PickupPoint1, PickupPOint2, ..

怎么做到这一点?这有可能吗?这是最佳做法吗?

How can this be done? Is this even possible? Is this a best practice?

推荐答案

您所描述的是工厂模式,所以是的,这是最佳做法。

What you are describing is the Factory pattern, so yes, it's a best practice.

http://www.devshed.com/c/a/PHP/Design-Patterns-in-PHP-Factory-Method-and-Abstract-Factory/

您的工厂本身不需要实现该接口。它可以是静态类甚至是函数:

Your factory itself does not need to implement the interface. It could be a static class or even a function:

class PickupPointFactory{
  public static function create($type){
    /* your creation logic here */
    switch ($type) {
      case "PickupPoint1" : 
        $obj = new PickupPoint1(); 
      break;
      case "PickupPoint2" :
        $obj = new PickupPoint2();  
      break;
    }
    return $obj;
  }
}

$newPoint = PickupPointFactory::create("PickupPoint2");

创建逻辑可以更通用,以避免每次添加类时更改工厂你的申请:

The creation logic can be a lot more generic to avoid changing the factory every time you add a class to your application:

class PickupPointFactory{
  public static function create($type, $options){
    /* your creation logic here */
    if(file_exists(dirname(__FILE__).'/'.$type.'.class.php')) {
      require_once(dirname(__FILE__).'/'.$type.'.class.php');
      $obj = new $type($options);
      return $obj;
    } else {
      throw new Exception('Unknown PickupPoint type: '.$type);
    }
  }
}

$newPoint = PickupPointFactory::create("PickupPoint2", array());

这假设您要在名为PickupPoint1.class.php的文件中创建类。工厂所在的目录,以及类中的构造函数只需要一个参数。

This one is assuming you are creating your classes in files named "PickupPoint1.class.php" in the same directory that the factory is, and that the constructor in your classes need only one parameter.

我没有测试这段代码,所以有些bug可能会漏掉。

I didn't test this code, so some bug might have slipped in.

这篇关于在PHP中创建一个接口,它可以指定一个类型来确定要使用的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆