I have made a class for generating html form elements:
class input
{
public $tag = 'input', $type = 'text', $customClass, $name;
public function __construct(array $cfg){
if ( isset($cfg['name']) ){
if ( isset($cfg['tag']){
$this->tag = strtolower($cfg['tag']);
}
if ( $this->tag === 'input' && isset($cfg['type']){
$this->type = $cfg['type'];
}
if ( isset($cfg['customClass']){
$this->customClass= $cfg['customClass'];
}
//etc...
}
}
}
I would like to be able to set a custom default config for all my fields - without changing the class. For example I want all the fields in a given script to have 'myClass' as customClass. For me, the solution would be to make these default values static, and make a function to change them like this:
class input
{
static $tag = 'input', $type = 'text', $customClass, $name;
public static function setDefault(array $cfg){
if ( isset($cfg['tag']) ){
self::tag = strtolower($cfg['tag']);
}
if ( isset($cfg['type']) ){
self::type = $cfg['type'];
}
if ( isset($cfg['customClass']) ){
self::customClass= $cfg['customClass'];
}
//etc...
}
public function __construct(array $cfg){
if ( isset($cfg['name']) ){
$this->tag = isset($cfg['tag']) ? strtolower($cfg['tag']) : self::tag;
$this->type = isset($cfg['type']) ? strtolower($cfg['type']) : self::type;
$this->customClass = isset($cfg['customClass']) ? $cfg['customClass'] : self::customClass;
//etc...
}
}
}
The problem is that I read everywhere - and particulary here - that static are evil. What other solution would be suitable in the present case? Or can someone tell me "yeah, that's the perfect case to use statics!"? :)