6

I'm developing a plugin and am just thinking over best practices. How common is it to have a global object? Currently on plugins_loaded I am creating a global object of my class:

add_action( 'plugins_loaded', array( 'Test_Plugin', 'init' ) );
...
public static function init() {
    global $testerski;
    $testerski = __CLASS__;
    $testerski = new $testerski;
}

This allows me to use the global $testerski to call any variables or methods. Is this a common practice?


My other concern is that since I am creating a global object and from my understanding, any hooks have to have public functions so WordPress can call them. The problem is that I can call these functions meant for hooks from my global object. For example, I have some hooks registered as:

public function __construct() {
    add_action( 'init',             array( $this, 'test_plugin_setup' )             );
    add_action( 'template_include', array( $this, 'test_templates' )                );
    add_filter( 'cron_schedules',   array( $this, 'test_add_monthly_schedule' )     ); 
}

public function test_templates( $template ) {
    ...
}

Which I could technically call using $testerski->test_templates(). Since this function shouldn't really be called directly and only used for hooks - is there a way I can prevent it from being called directly like this? Is something like this dangerous or am I overthinking it?

I've noticed some hooks have warnings, like wp_enqueue_scripts will give you a notice that it was called incorrectly.

  • 1
    I'm just wondering, it might be a good idea to submit your full code to Code Review as well. I got a couple of really good suggestions regarding OOP and classes there which helped me a lot ;-) – Pieter Goosen Jan 26 '16 at 18:07
  • 1
    @PieterGoosen I posted a question here. – Howdy_McGee Jan 26 '16 at 19:39
  • @Howdy_McGee Did you see this post from Otto? – Tim Malone Jun 30 '16 at 5:52
  • Was this ever resolved? – Andy Macaulay-Brook Oct 7 '16 at 19:22
1

This is not common practice, but it works. A better approach, is to use a class and with the singleton pattern, just like WooCommerce and many others, where you have:

  • A static function (called instance, getInstance...) that:
    • Creates an instance (object) if not already done and returns it
    • Or returns the existing instance

Let's continue with the WooCommerce example; we used to do this to access the global object:

global $woocommerce; 

Now we do:

WooCommerce::instance();

# Or with the handy Shortcut
WC();

I think you will enjoy reading these:


You can check the value of current_filter() inside your method, but if I were you, I wouldn't bother. This is not a threat, and other developers may want to use your code, so don't block them.

  • 1
    Thanks! I don't know a ton of OOP and Singletons are new to me so it's a cool concept. I do find it funny that the wiki references are commenting on how bad Singletons are but they also say that globals are bad so I'll take it with a grain of salt. I also have a similar question on Code Review if you wanna get points there too :) – Howdy_McGee Oct 20 '16 at 19:26

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.