Forums

Registering Class methods as hook callbacks (3 posts)

  1. jcnetsys
    Member
    Posted 11 months ago #

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

  2. jcnetsys
    Member
    Posted 11 months ago #

    I've taken a look at the source - WordPress doesn't do anything special with the variable. It just calls 'call_user_func_array' with the function name as a parameter.

    So basically I now need a way of getting the variable that the instance has been assigned to from within the class like:

    class bob()
    {
         public function getInstance()
         {
                return object name//THIS IS WHAT I DON'T KNOW
         }
    
    }
    
    $gary = new bob();
    
    $gary->getInstance();//returns 'gary'
  3. jcnetsys
    Member
    Posted 11 months ago #

    I was correct. You can pass $this as a parameter.

Reply

You must log in to post.

About this Topic