Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts

    Class Method with call_user_func

    Can anyone tell me how this can be done?
    I'm trying to invoke a private function in my class with another private function using the call_user_func function:

    call_user_func($this->$replacement_func,$replacement_params);

    The call_user_func function should (theoretically) call the private function, but instead is throwing this error:

    First argument is expected to be a valid callback

    Which obciously means that this is the wrong way of doing this. Does anybody know the proper way of going about this?

    EDIT:

    Here's how you would do this:
    call_user_func(array("classname",$replacement_func),$replacement_params);
    Where classname is the name of your class.

  • #2
    New Coder
    Join Date
    Feb 2010
    Location
    UK, North West
    Posts
    36
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Hello,
    You need to pass the instance in by an array too like the following:


    Here's an example where I've used it:
    PHP Code:
            /**
            * DetectRequestType     - Checks to see all the keys and values of the post request
            * as the post can contain more than one request, look through the ones we need for 
            * returned data.
            *
            * $param $postReq        - Reference of the $_POST superglobal to search.
            */
            
    public function DetectRequestType(&$postReq)
            {
                foreach(
    $postReq as $key => $post)
                {
                    
    # Find first and only valid request to return data on ^^
                    
    if(in_array($key, (array)$this->_postTypes))
                    {
                        
    $methodName = (string)"Get" $key "Data";
                        
                        if(
    method_exists($this$methodName))
                        {
                            return (string)(
    call_user_func(array($this$methodName), intval($post)));
                        }
                    }
                }
            } 


  •  

    Posting Permissions

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