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.

3 s
3

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

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *