Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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')?

share|improve this question
3  
Wouldn’t a test be faster than asking the question? ;) Yes, you can do this. –  toscho Jun 15 '11 at 11:10
2  
A test can help, but sometimes you look for feedback as well. I think it's okay to actually ask the question, and there is this rule, questioning the OPs motivation ... :) –  hakre Aug 17 '11 at 23:06
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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