
Originally Posted by
Shrike
I guess the question is what needs to be configured? If you are just setting database connection strings and so forth I'd say constants are the easiest and cleanest approach.
I'd argue that constants are among the worst configuration because:
1. If you use global constants with define() they (can) clash in the global namespace
2. If you define constants in your class it requires editing of your class files every time you / your users wanna change configuration.
Sometimes a constant is in place, for example to set the base dir of your library, maybee in a profile/include-file that inits the library. I would however argue that a separate configuration object (not a singleton) is probably the best in most cases, something like:
PHP Code:
<?php
class ClassConfig{
protected $path;
setPath($path){
$this->path = ( substr($path,-1) == DIRECTORY_SEPARATOR ) ? $path : $path . DIRECTORY_SEPARATOR ;
}
}
?>
Because it lets you (as in the example above) encapsulate configuration logic(such as tidying up paths adding / or \ on the end if it's missing, etc.) in one place. Then just use the config object somewhat like this:
PHP Code:
<?php
$cfg = new ClassConfig;
$cfg->setPath("./home/thr"); /* and the path will be fixed as we make sure it is in the setPath() method. */
$realClass = new RealClass($cfg);
?>
Bookmarks