What is the best way, using PHP, to design a modular system that can be added to as easily as possible? For example, if you had a shopping cart, you might have a payment base class that has all your required methods. Then you can have a Paypal class that extends that class, and a Visa/Mastercard that also extends the base class, but how do you actually put them in use?
If you did a functional version of the cart, you could make a function called doExecute and pass in the required information. Then you name all the files as Payment_Method.php. The file names are then read, creating a list of acceptable payment methods. Once one is picked, that file is the only one included in the script, and then the doExecute method is called guaranteeing that the method in the payment method include file is the one called.
However, for an object oriented approach, that really does work that well. How do you add in the additional methods without having to keep modifying the code to include objects of the new classes?
The way I’ve implemented payment systems like this is to have the base class, as you mentioned, but have the base class instantiate the required payment vendor, which extends the base class. Then the base class is in control of what file gets loaded, centralizing control, logging, etc.
Since all vendors have different data submission structures. Your base class should have a function to “load” the data. This put it in your standard format. Each payment class would then have a “map” function to map your structure to their structure. For example, splitting up dates into separate year, month, day fields.
You then have a “submit” function to submit the data to the processing vendor. Then a “parse response” function to parse the response and put it in a standardize response data structure you come up with.
For simplicity, you can have a single method that calls all three of the functions (map->submit->map. But for testing, it easier to have them separate.
$processor->mapData();
$processor->submit();
$response = $processor->parseResponse();
Of course, this doesn’t really work with Paypal. Paypal is asynchronous, you send the user to their site. Paypal then sends them back to your site when they have done processing. Google checkout is the same way.