8

I'm a newbie at PHP and WordPress, so please be gentle!

I'm building a plugin - following a few tutorials, getting it all working.

My question is why is the function I want to call wrapped up in the array($this, function)? For example I have this constructor code inside my class

 function __construct() {
      add_action( 'admin_init',array( $this, 'getStuffDone' ) );
 }

The getStuffDone function gets fired OK - but why does it have to be in the format it is in?

Any insight is much appreciated.

4
  • 1
    I don't know enough about OOP to give an informative answer but some research gives a few good references: Using add_action with Class and PHP Passing to Functions
    – Howdy_McGee
    Apr 16, 2015 at 16:54
  • Never register callbacks in constructors. That makes your code untestable.
    – fuxia
    Apr 16, 2015 at 18:52
  • @toscho link to an example correct initialization setup? Call a dedicated init() function or something from the constructor?
    – lkraav
    Apr 17, 2015 at 18:00
  • @lkraav See gmazzap's answer to that question.
    – fuxia
    Apr 17, 2015 at 19:05

4 Answers 4

6

It's a PHP callback. You need the syntax to keep a reference to the class instance.

Put it this way - if you didn't have $this, how does the caller know that getStuffDone is a method of your class, and not just a regular PHP function? It doesn't.

Using array( $this, 'getStuffDone' ) says to PHP:

Hey bro, you need to call the method getStuffDone on this instance of my class

2
  • What I don't understand, which may be how I'm reading it, but the first sentence under Passing says except language constructs such as: array(), but here we are passing an array. Is this a typo or am I reading the sentence wrong?
    – Howdy_McGee
    Apr 16, 2015 at 20:40
  • No it's right, but it's referring to functions, not methods. Read the para directly below it. Apr 17, 2015 at 8:17
5

the add_action() function definition looks like:

 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
     return add_filter($tag, $function_to_add, $priority, $accepted_args);
 }

so according to that it looks like it expects (string,string,int,int)

In a traditional php file the second parameter would simply be one of your function names.

so you would have add_action('admin_init','my_init_function')

It looks like you're using a class to encapsulate your plugin. Other classes could have functions in them with the same name (getStuffDone).

So your function is only known with reference to your class, that's why you have to specify your class $this as well as the function name.

If you're trying to reference a function inside a class you have to use array callable syntax

See also codex.wordpress.org

1

See the documentation, under the "User Contributed Notes":

"To use add_action() when your plugin or theme is built using classes, you need to use the array callable syntax. You would pass the function to add_action() as an array, with $this as the first element, then the name of the class method ..."

1

For future readers this example demonstrates the reason for the syntax. Although possibly a bad practice since the class method is called inside the add_test function without initializing the referenced class.

function add_test( $reference ){
  $object_instance = $reference[0];
  $object_method = $reference[1];
  $to_return = $object_instance->$object_method();
  return $to_return;
}
class test{
  public function add(){
    $a = $this->get_first();
    $b = add_test(array($this,'get_second'));
    return $a+$b;
   }
   
  public function get_first(){
    return 1;
  }
  
  public function get_second(){
    return 2;
  }
}
$test_instance = new test();
echo $test_instance->add();

We're simply storing the reference of the object where the method resides so the function add_action can successfully execute the method in question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.