0

I don't think this is the right way to do things, maybe I'm wrong or missing something but to me it seems unnecessary. Is this correct? What are the implications if any of doing things this way?

In the functions file all the functions are wrapped in a class:

class This_Theme
{
   function example_fucntion(
      echo 'example';
   );
}

Then in the theme they call the function statically:

This_Theme::example_function();

Isn't this pointlessly wrapping the function in a class to call it statically? Wouldn't it make more sense to just leave out the class?

7
  • 1
    One use case of this is helping avoid function name collision. For example, if a plugin also creates a function called example_function then you will receive a fatal error Nov 6, 2015 at 17:37
  • I did think of that. The reason I asked is because when debugging the theme I get 500 notice errors saying not to call the function statically so I figured this just could not be right.
    – JacobRay
    Nov 6, 2015 at 17:40
  • 1
    Based on your example code it is hard to say. In a lot of cases, yes it is almost a waste to do this as you show. In my opinion, most people do this as a sort of sudo namespace. However there is a really good talk on wordpress.tv about using OOP principles while theming. wordpress.tv/2015/01/14/… Nov 6, 2015 at 17:41
  • 1
    The notice about calling the function statically is result of poor class construction not an inherent problem with using classes.
    – s_ha_dum
    Nov 6, 2015 at 17:47
  • I agree with @s_ha_dum, the notice you get is a pure PHP coding issue, nothing to do with WordPress. A PHP class method that is not statically declared can not be called statically. I think this was introduced in PHP 5.4 (not 100% sure), a PHP version released near 4 years ago and, since one month ago, a obsolete version with no active support. You may start thinking in updating your code to current standards.
    – cybmeta
    Nov 6, 2015 at 17:55

1 Answer 1

1

It is a matter of opinion and coding style. This kind of coding style is very popular in wordpress plugin and theme development therefor I will not fault anyone for doing it but I agree that the practice of having classes that have only static method or used as a singleton in the context of wordpress is just BS.

This was justified when PHP versions with name spacing weren't popular enough, and wrapping code in a class basically provided the name spacing, but now that the last version without name spacing was EOLed almost 5 years ago the practice is ridiculous, and just makes some kinds of coding aspects harder and too verbose.

But in the end this is the developers code, and he needs to write the code in a way that will be cleaner and more maintainable for him and if he prefer OOP coding over functional then there is nothing terribly wrong with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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