I think that is an example of "favouring composition … ", which is a good principle to follow.
But still, you end up with “settings” as Jake calls them.
I evolved a means of storing some of these settings ( for the back-end of a multi-user, single-server CMS ) and would like to know what others think of my approach.
projectA_config.php
<?php
/**
* testing a static class instead of voodoo-ish includes
* where values pop up from f knows where
*
* Config file for each client
* WSC_ = WebSiteControl
**/
class BadMethodException extends Exception {
function __construct( $msg ){
$this->message = 'That information-type ' . $msg . ' does not exist in settings' ;
echo $this->msg ;
}
}
include 'iwsc_config.php' ;
/**
* ProjectA WSC settings, a concrete static class
* holding admin settings
**/
class WSC_Config implements IWSC_Config {
/**
* static properties - accessors must exist for each
* this means
* -this class must conform to the interface
* -the methods are findable
*
* -scalar values first
* =then methods with content
* -then accessors
**/
private static $PROJECT_TITLE = "Project A";
final static function getMapLimits(){
$map_limits['lat']['max'] = 50.1 ;
$map_limits['lat']['min'] = 51.2 ;
$map_limits['lng']['max'] = -0.5 ;
$map_limits['lng']['min'] = -0.6 ;
return $map_limits ;
}
final static function getProjectTitle(){
return self::$PROJECT_TITLE ;
}
/**
* function __callStatic() only works from 5.3 on
*
*
**/
function __callStatic( $method, $args=0 ){
throw new BadMethodException( $method ) ;
}
} // end class
I just got so fed up of trying to find out where the hell all these globals were coming from that I put them into a static class, and made that static class implement everything in an interface.
That has proved to be very liberating because now I see lines like this:
$map = WSC_Config::getMapLimits();
And at least I know where things are coming from.
I can use reflection to find all my settings.
If I misspell a call (eg getMapsLimit() ) I get a big warning straight away.
If I create a new project and miss out a setting I get a big warning straight away.
It’s verbose, if I decide a new setting is needed, I have to edit the Interface, create the property and an accessor.
I should probably then make this static class available through a registry, if I have understood correctly.
Its the only time I have really used Statics in my code (apart from libraries like PEAR) because Statics never really seemed necessary, but in this case I find it invaluable - but is this a good example of Statics really being put to its good use, a central bag of global “settings”?