I have been using "static" classes as a method to group functions with relating purposes under a common name that provides readability and maintainability to the code at the cost of performance and possibly incorrect usage of the object model caused by ignorance.
I can absolutely do that with a normal class but when the object would have no state it makes no sense instantiating it every time I need to access a function, it also feels wrong.
With php 5.6 or something they introduced namespaced functions, but even if that was not the case I could still fake a namespace with name prefixes, so (prepare, ignorance incoming) I guess that change only adds to php's internal function management system or whatever it is.
I was thinking - which would be a proper way to declare functions of such nature?
For an example I will take this html helper that I had created a long time ago. You can see it has a bunch of functions like
class HTML {
public static function script($src, array $attributes = array(), $defer = true){
$attributes['type'] = 'text/javascript';
$attributes['src'] = strstr($src, '://') ? $src : SITE_URL . $src;
if($defer){
$attributes['defer'] = 'defer';
}
return '<script'.self::parseAttributes($attributes).'></script>';
}
public static function stylesheet($src, array $attributes = array()){
$attributes['type'] = 'text/css';
$attributes['href'] = strstr($src, '://') ? $src : SITE_URL . $src;
$attributes['rel'] = 'stylesheet';
return '<link'.self::parseAttributes($attributes).'/>';
}
}
but the object itself has no state, so wouldn't it be better to just have these functions be something like
function htmlhelper_script($src, array $attributes = array(), $defer = true){
$attributes['type'] = 'text/javascript';
$attributes['src'] = strstr($src, '://') ? $src : SITE_URL . $src;
if($defer){
$attributes['defer'] = 'defer';
}
return '<script'.self::parseAttributes($attributes).'></script>';
}
function htmlhelper_stylesheet($src, array $attributes = array()){
$attributes['type'] = 'text/css';
$attributes['href'] = strstr($src, '://') ? $src : SITE_URL . $src;
$attributes['rel'] = 'stylesheet';
return '<link'.self::parseAttributes($attributes).'/>';
}
What are the differences between the two approaches, which one would be considered better and why?