vendredi 27 avril 2018

Is this PHP pseudo-DI pattern wrong / dangerous?

I have let's say a class, VehicleManager. This class manages the lifecycle and calling of Vehicle type Objects. Vehicles act as modules and extend the base Vehicle class.

So if you make a class bike, you never do new Bike(), you do VehicleManager->create('Bike',5);

My problem is, i want the construction of every vehicle to receive predefined parameters,and do some predefined actions, so my base Vehicle class has a private final constructor.

Example:

final private function __construct(Array $myCustomData){
  $this->customData = $myCustomData;
}

But still i want each vehicle type to have some custom initializion

So after doing

$newVehicle = new $ClassName($myCustomData);

Im doing

$newVehicle->initialize(); 

Is this acceptable? Thanks

Aucun commentaire:

Enregistrer un commentaire