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{
...
}
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:
The creation logic can be a lot more generic to avoid changing the factory every time you add a class to your application:
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.
I didn’t test this code, so some bug might have slipped in.