I’ve created a plugin, and of course being me, I wanted to go with a nice OO approach. Now what I’ve been doing is to create this class and then just below create an instance of this class:
class ClassName {
public function __construct(){
}
}
$class_instance = new ClassName();
I’m assuming there is a more WP way to have this class initiated, and then I came across people saying that they prefer to have an init()
function than a __construct()
one. And similarly I found the a few people using following hook:
class ClassName {
public function init(){
}
}
add_action( 'load-plugins.php', array( 'ClassName', 'init' ) );
What is generally considered the best way to create a WP class instance on load and have this as a globally accessibly variable?
NOTE: As an interesting side point, I’ve noticed that while register_activation_hook()
can be called from within the __construct
, it cannot be called from within the init()
using the second example. Perhaps someone could enlighten me on this point.
Edit: Thanks for all the answers, there clearly is a fair bit of debate as to how handle the initialization within the class itself, but I think there’s generally a pretty good consensus that add_action( 'plugins_loaded', ...);
is the best way to actually kick it off…
Edit: Just to confuse matters, I’ve also seen this used (although I wouldn’t use this method myself because turning a nicely OO class into a function seems to defeat the point of it):
// Start up this plugin
add_action( 'init', 'ClassName' );
function ClassName() {
global $class_name;
$class_name = new ClassName();
}
Good question, there are a number of approaches and it depends on what you want to achieve.
I often do;
add_action( 'plugins_loaded', array( 'someClassy', 'init' ));
class someClassy {
public static function init() {
$class = __CLASS__;
new $class;
}
public function __construct() {
//construct what you see fit here...
}
//etc...
}
A more thorough an indepth example which came about as a result of some recent discussions on this very topic within the chat room can be seen in this gist by WPSE member toscho.
The empty constructor approach.
Here is an excerpt of advantages/disadvantages taken from the above gist which exemplifies the empty constructor approach in full.
Advantages:
Unit tests can create new instances without activating any hooks
automatically. No Singleton.No global variable needed.
Whoever wants to work with the plugin instance can just call
T5_Plugin_Class_Demo::get_instance().Easy to deactivate.
Still real OOP: no working methods are static.
Disadvantage:
- Maybe harder to read?
The disadvantage in my opinion is a weak one at that which is why it would have to be my favored approach, however not the only one I use. In fact several other heavy weights will no doubt chime in on this topic with their take on the subject shortly because there are some good opinions surrounding this topic that should be voiced.
note: I need to find the gist example from toscho that ran through 3 or 4 comparisons of how to instantiate a class within a plugin that looked at the pros and cons of each, which the above link was the favored way to do it, but the other examples provide a good contrast to this topic. Hopefully toscho still has that on file.
Note: The WPSE to this topic with relevant examples and comparisons. Also the best solution for instance a class in WordPress.
add_shortcode( 'baztag', array( My_Plugin::get_instance(), 'foo' ) );
class My_Plugin {
private $var="foo";
protected static $instance = NULL;
public static function get_instance() {
// create an object
NULL === self::$instance and self::$instance = new self;
return self::$instance; // return the object
}
public function foo() {
return $this->var; // never echo or print in a shortcode!
}
}