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.