0

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?

  • In most languages I know, no difference whatsoever, which is why static methods should be fairly uncommon. But PHP isn't my best language. – Ixrec May 17 '16 at 14:17
  • 1
    PHP namespaces have nothing to do with static functions. Your static HTML helper is fine especially since you are also using globals. If you ever want to migrate to more testable and easier to maintain code then you might start looking more into oop and dependency injection. – Cerad May 17 '16 at 15:29
2

Neither approach is functionally better: the both do the job (except the self keyword would need to go in your namespace-based example).

So the difference lies essentially in clarity and maintainability. If your intention is that none of these functions have any shared state whatsoever, I think that clearest approach is the one that explicitly forbids it: namespaces and functions.

Also: namespaces were added in PHP 5.3 (not 5.6) so it's pretty safe to rely on them by now. Namespacing is more powerful then prefixing since it has importing and aliasing features so I'd go with namespacing rather then prefixing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.