I’m not sure if I have done this correctly.
As I understand it:
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.
If I have an instance of a class in $foo and want to call the method bar I pass the array array($foo,'bar').
If I need to register an action inside the class itself would it work with array($this,'bar')?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
For static methods you can also do this:
['foo','bar']
when the following gives Undefined class constant ‘bar’:
foo::bar
example – when specifying the $control_callback for wp_add_dashboard_widget
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0