0

This question is about the famous debate on using Dynamic methods over static methods in classes. According to my research the widely acknowledged idea is to limit the use of static methods as much as we can.

Now in terms of WordPress I have a class that registers a shortcode. The shortcode has some logic built into it and is dependent on fetching some meta values from the page it is being used on.

Now I have recently found out that there is also a widget in the sidebar that needs to use those same metavalues according to the same logic used in the shortcode.

For Ex: In the shortcode class I have a method that grabs all meta values and stores them in an array with a fixed index name that I need the value to correspond to. So my array after fetching is something like:

$locations = array( 'slug' => 'new-york', 'name' => 'New York', 'zipcode' => '123456');

My Problem:

1- If I create a new instance of the class in the sidebar I am worried about the one 'shortcode' and two 'init' hooks within the constructor and I am worried weather they would affect performance.

2- Instead of fetching the post meta one time. Now I am fetching it two times for the same value. One in the shortcode and other in the sidebar.

I need you guys to help me advice a better structure to solve this formula so both of these problems are minimized.

2

This question is about the famous debate on using Dynamic methods over static methods in classes. According to my research the widely acknowledged idea is to limit the use of static methods as much as we can.

If you need to store something it'd need to be a static member variable, which is problematic. Just use dynamic methods, they have many advantages over static, and static has very few advantages and lots of known disadvantages. But as you said this is a debate, and if that is correct then it has no place here. Stack Exchange is a Q&A site, not a discussion forum, you need to be able to mark an answer as canonically correct

1- If I create a new instance of the class in the sidebar I am worried about the one 'shortcode' and two 'init' hooks within the constructor and I am worried weather they would affect performance.

If your init routine is expensive enough that doing it twice impacts performance, then something is seriously wrong with your init routine. Are you making remote requests or fetching thousands of posts ( or fetching a handful of posts via WP_Query with post meta parameters? )

Even then, just test it. Implementing it then comparing the page loading times and query counts isn't difficult, use a plugin such as query monitor to do it

2- Instead of fetching the post meta one time. Now I am fetching it two times for the same value. One in the shortcode and other in the sidebar.

This is a non-issue

Ignoring that fetching a post meta value by it's key and post ID is a very fast query, all of this is cached in memory. If you have an object cache it will even persist across requests.

In fact, WP pre-emptively fetches the post meta for you to save time by grabbing all of a posts meta in the same query.

Ironically, those people who try and limit what WP_Query fetches so that they can only query for what they need are the ones with the slowdowns due to lots of small queries to fetch things

So no, post meta is only fetched once, and cached. To fetch it twice you'd need to write the raw SQL to do so

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.