Results 1 to 4 of 4

Thread: [RESOLVED] Callback Functions

  1. #1

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Resolved [RESOLVED] Callback Functions

    I'm having an issue with callback functions, specifically storing them...

    If I have a class:

    PHP Code:
    class myObject{

        protected 
    $callback;

        public function 
    __construct($callbackFunction){
            
    $this->callback $callbackFunction;
            .
            .
            .
        }

        public function 
    doSomething($args){
            if(
    is_callable($this->callback)){
                return 
    $this->callback($args);
            }
        }


    And I try doing this:

    PHP Code:

    function foo($arg){
        return 
    strtoupper($arg);
    }

    $myNewObject = new myObject("foo");
    echo 
    $myNewObject->doSomething("bar"); //should echo BAR 
    I end up with an error:

    Code:
    Fatal Error: Call to undefined method MyObject::callback() in ...
    The is_callable() is succeeding, but the actual callback function call is the line failing with that error.

    This isn't exactly what I'm doing obviously, but its a replicate of the code. Any ideas?
    IWS

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Callback Functions

    you have to tell PHP that you're calling $this->callback (the variable), not the method callback() of $this.

    PHP Code:
    return {$this->callback}(); 

  3. #3

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Re: Callback Functions

    PHP Code:
    return {$this->callback}(empty($this->buffer) ? $template $this->buffer); 
    It's not liking that.. unexpected {

    I wanna stray from call_user_func() too, because that is hugely processor intensive.
    Last edited by tomcatexodus; Jun 16th, 2010 at 12:34 AM.
    IWS

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Callback Functions

    oh. store it in a non-class variable then:

    PHP Code:
    $callback $this->callback;
    $callback(); 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width