I am creating a class for processing IPP Paypal as part of the project, and since I already know that I will need to use it again in at least the next two upcoming assignments - I want to make sure that I structure it in such a way that allows me reuse it without having to recode the class - I just want to handle the changes in business logic.
The first part of the question is re. interfaces. I did not quite understand their usefulness and when / where to deploy them. If I have a class file ("class.paypal-ipn.php"), should I implement the interface in this file?
Here is what I'm working so far (the list of functions is not complete, but its just to illustrate):
CLASS.PAYPAL-IPN-BASE.PHP
interface ipn_interface { //Database Functions // Actual queries should come from a project-specific business logic class // so that this class is reusable. public function getDatabaseConnection(); public function setDatabaseVars($host="localhost",$user="root",$password="",$db="mydb"); public function dbQuery($SQL); //Logging Functions public function writeLog($logMessage); public function dumpLogToDatabase(); public function dumpLogToEmail(); public function dumpLogToFile(); //Business Logic Functions private function getTransaction($transactionID); //Misc Functions public function terminate(); } class paypal_ipn_base { //nothing to do with business logic here. public function getDatabaseConnection() { } public function setDatabaseVars($host="localhost",$user="root",$password="",$db="mydb") { } public function dbQuery($SQL) { } }
CLASS.PAYPAL-IPN.PHP
final class paypal_ipn extends paypal_ipn_base implements ipn_interface {
Using
In this project:
- Require class files for both the base and business logical classes.
- Instatiate * paypal_ipn *
- Enter code
In other projects:
- Copy over IPN base class
- Change / rewrite the business logic class * paypal_ipn * within the limits of the interface.
- Instant * paypal_ipn *
- Enter code
So, as you can see, I literally just use it to define groups of related functions and add comments. This makes reading easier, but what (if there is) another advantage for me is that I can combine the expander and the base class and cause errors if something is missing?
stdClass Question
The second part of the question is based on the aspect of readability. Inside the class itself, the number of stored variables is constantly growing, some of them are set in the constructor, some by other functions - they relate to such things as storing connection shafts to the database (and the connection resource itself), should the code be executed in test mode, settings logging and the journal itself, and so on ...
I started just creating them as usual (again, below incomplete and for illustration):
$this->dbConnection = false; $this->dbHost = ""; $this->dbUser = ""; $this->enableLogging = true; $this->sendLogByEmail = true; $this->sendLogTo = " user@domain.com ";
But then I thought that a growing list might work with some kind of structure, so I adapted it for:
$this->database->connection = false; $this->database->host = ""; $this->database->user = ""; $this->logging->enable = true; $this->logging->sendByEmail = true; $this->logging->emailTo = " user@domain.com ";
Which makes it a lot easier for me to read the variable list when I unload the whole class due to code and test.
As soon as I finish, I plan to write a project extension for the general class, where I will store the actual SQL for queries - as from one project to another, the IPv procedure and Paypal logic will not change - but each project database structure will be, so the class extension will deactivate everything back to one format, so the base class will not have to worry about it and will never have to change it after writing it.
So, everything is just a sanity check - before I went too far along this road, does this seem like the right approach?