Take the 2-minute tour ×

So is it possible to do something like this with add_action?

class class{}
$my_class = new class;
add_action('init', 'my_class');
share|improve this question
Have you looked at the example on the add_action codex page? –  Milo Dec 30 '12 at 16:54
See this answer for different possibilities. –  toscho Dec 30 '12 at 17:10
add comment

1 Answer

up vote 2 down vote accepted

You can do this:

class testclass() {
  function test() {
    echo 'howdy';

Or this:

$t = new testclass();

It doesn't work like...

$t = new testclass();
// or this either, for good measure
$t = new testclass();

.. but I am not sure what you are trying to accomplish by using that pattern. You've already instantiated the class so the constructor, if present, has already ran. Without a callback method, I don't know what you expect to happen.

share|improve this answer
Small and usefull answer. –  bueltge Dec 30 '12 at 17:24
The first method doesn’t pass E_STRICT, and the last needs a good explanation. :) –  toscho Dec 30 '12 at 20:50
add comment

Your Answer


