No, you cannot ‘initialise’ or instantiate the class through a hook, not directly. Some additional code is always required ( and it is not a desirable thing to be able to do that, as you’re opening a can of worms for yourself.
Here is a better way of doing it:
class MyClass {
function __construct() {
add_action( 'admin_init', [ $this, 'getStuffDone' ] );
}
function getStuffDone() {
// .. This is where stuff gets done ..
}
}
$var = new MyClass();
Of course one could create an interface class to simplify it for the general case even further:
class IGetStuffDone {
function IGetStuffDone(){
add_action( 'admin_init', [ $this, 'getStuffDone' ] );
}
public abstract function getStuffDone();
}
Note that as an interface, you can’t create an object of this type directly, but you could create a sub-class, letting you say:
class CDoingThings extends IGetStuffDone {
function getStuffDone(){
// doing things
}
}
$var = new CDoingThings();
Which would then automatically add all the hooks, you just need to define what exactly is being done in a subclass and then create it!
On Constructors
I wouldn’t add a constructor as a hook function, it’s bad practice, and can lead ot a lot of unusual events. Also in most languages a constructor returns the object that is being instantiated, so if your hook needs to return something like in a filter, it will not return the filtered variable as you want, but instead it will return the class object.
Directly calling a constructor or a destructor is very, very, very bad programming practice, no matter which language you’re in, and should never be done.
Constructors should also construct objects, to initialise them ready for use, not for actual work. Work to be done by the object should be in a separate function.
Static class methods, and not needing to instantiate/initialise at all
If your class method is a static class method, you can pass the name of the class in quotes rather than $this
as shown below:
class MyClass {
public static function getStuffDone() {
// .. This is where stuff gets done ..
}
}
add_action( 'admin_init', [ __NAMESPACE__ . '\MyClass','getStuffDone' ] );
Note the use of __NAMESPACE__
which is required if your class is inside a namespace.
Closures
Sadly you cannot avoid the line creating the new class. The only other solution to skipping it would involve boiler plate code that still has that line e.g.:
add_action( 'admin_init', function() {
$var = new MyClass();
$var->getStuffDone();
} );
At which point you may as well skip the class, and just use a function:
add_action( 'admin_init', function() {
// do stuff
} );
But keep in mind you have now introduced the spectre of anonymous functions. There is no way to remove the above action using remove_action
, and this can and does cause great pain for developers who have to work with other peoples code.
On Ampersands
You may see actions used like this:
array( &$this, 'getStuffDone' )
This is bad. &
was added back in PHP 4 when objects were passed as values, not as references. PHP 4 is more than a decade old, and hasn’t been supported by WordPress in a very long time.
There is no reason to use &this
when adding hooks and filters, and removing the reference will cause no issues, and may even improve compatibility with future versions of PHP
Use this instead:
[ $this, 'getStuffDone' ]