I want to produce maintainable code for configuring one or multiple classes differently depending whether in a production or development environment.
For example, making a Paypal
class/object that handles API calls to Paypal, using different settings depending on some sort of global switch set to either Dev or Live. The first would connect to the sandbox account, the other the live account.
I want to keep it as simple as possible – I would not be loading configuration variables from a file or database, but instead specifying them in either a subclass’s constructor, one subclass for dev and one for live, or similar:
class Paypal {
private $endpointUrl;
public static function makeSandbox(){
return new Paypal('http://....');
}
public static function makeLive(){
return new Paypal('http://....');
}
// ....
public function Paypal($url){
$this->endpointUrl = $url;
}
}
However this leaves me with so many questions, like for other classes who’s configuration is different between live/server, do I store the different initialisations in it’s class, or should I have a class dedicated to all configuration details, should it be a singleton, and so on.
The application will be rather simple, and only be maintained by myself so the idea of XML configuration files etc seems over the top, a PHP configuration file with a switch statement and global variables seems like bad form. Using a sort of global Config
class brings a whole load of questions. I’m feeling pretty overwhelmed here. I’ve read around and there seem to be so many opinions.
The one rule that sticks in my mind is use OOP patterns to solve a problem, not just for the sake of it. Being a very small site with just me developing it, I’m very hesitant introducing multiple extra classes or objects just for dealing with two sets of global configurations.
Please help