1

I've created a plugin using various classes. I want to be able to access the methods on several of the different classes from within the page templates of my custom theme. The method I'm currently using is to make static methods but I'm wondering if this is the best way to do it?

An example could be a class with a method that checks whether the user is logged in and what their user role is. Let's call the class Plugin_Access and the method has_access();

Now let's say I need to customize content in the header, page template and footer using if() statements based on has_access(). To do this I would use if( Plugin_Access::has_access() ). To me I feel like this isn't the cleanest solution. I'm essentially calling the same method and getting the same result 3 times. If it had a call to the database within the method, this would be 3 calls rather than 1.

One solution I thought of was to make the class a singleton and to store the result of the function in a variable within the class. Then the method would check this variable and if it's null, then complete the rest of the method, otherwise just return the stored variable.

I feel like I'm definitely missing something. Thanks in advanced for any advice.

0

Premature optimization is the root of all evil (ok, waste of programing time). Why would you care how many times a function is called? it is not like the memory or CPU is getting "weaker" by overusing it.

It is more important that the code you produce will be self documenting as much as possible than doing micro optimizations, and if it means 3 calls instead of one, then so be it.

If you have an actual performance issue because the function does some heavy/slow computation, the answer is to "internally" cache the heavy/slow parts.

Side note 1. Use Wordpress APIs, do not reinvent wheels unless you have researched and found no other way as wordpress will do a lot of caching for you and your 3 DB access will in reality be only one in any case.

Side note 2. Singletons are rarely OOP and you will get a much easier to read and manipulate code, especially in the context of wordpress, if you just avoid classes and use namespaces in the first place.

0

As you understood, it depends of what your method will do.

If the method has to check the DB to define which boolean to return, like checking an option, then store its value in a property at hydratation or at first time you call the method. Tho, note that WP caches query results like get_option(), get_post_meta(), etc... So in such cases, I don't think there would be a benefit...
Otherwise, call it this way in your template without storing it in a property.

If the method has to return a DB query result, then instead, it's natural to store the result as a property of your domain model object, which by definition, should not be a singleton.
In WP, WP_Post is an example of domain model class. As you can see, $post->title returns a query result.
You hydrate a domain model object at its initialization.

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.