Registering Class methods as hook callbacks

If I need to register an action inside the class itself would it work with array($this, 'bar')?

Yes, it works. $thisDocs is referring to the concrete instance needed for the callback. That’s exactly like the $foo example you give. It’s just that $this is bit more special, but it represents basically the same and it works flawlessly with callbacks in PHP.

Additional:

if I have a class foo and a static method bar I can register that as the callback by passing the array array("foo","bar") as the function name.

Yes you can do so, for the static function, you can write it as a string instead of the array as well: foo::bar, see Callbacks Docs. Might be handy.

Leave a Comment

error code: 523