扩展类不需要相同的参数

时间:2019-11-26 10:30:34

标签: php interface abstract-class

我的类的实现方式存在问题 我有一个实现接口的抽象类:

interface DriverInterface {

   public function __construct(
      array $tableGateway,
      Sql $sql,
      $dbName,
   );

   public function process();

   public function setIdentiteService(IdentiteService $service);
   public function setPhotoService(PhotoService $service);
   public function setDocumentService(DocumentService $service);
   public function setLotService(LotService $lotService);
}

抽象类:

abstract class AbstractDriverMigration implements DriverInterface {

   protected $tableGateway;
   protected $sql;
   protected $dbName; 

   public function __construct(array $tableGateway, Sql $sql, $dbName)
   {
      $this->tableGateway = $tableGateway;
      $this->sql = $sql;
      $this->dbName = $dbName;
   }

   abstract public function process();

   public function setIdentiteService(IdentiteService $service) {}
   public function setPhotoService(PhotoService $service) {}
   public function setDocumentService(DocumentService $service) {}
   public function setLotService(LotService $service) {}
}

然后我以编程方式调用扩展抽象类的类:

$className = 'Migration\\Driver\\' . str_replace(' ', '', ucwords(str_replace('_', ' ', $class)));
$driver = new $className(...);

但是问题是,对于某些类(但不是全部),我需要使用从抽象类中覆盖的“ setXService”函数传递服务,最终我要测试传递服务的类。而且我不喜欢那样做。

if ($class == 'user' ) {
   $driver->setLotService($service)
}

我不想将服务传递给所有类,因为其中一些不需要它们。

0 个答案:

没有答案