Search code examples
phpphp-5.2

How can I pass a function as an optional variable to call_user_func in PHP


So I'd like to use the call_user_func to pass data to an optional parameter of a function.

Here's the example of a code, the optional parameter $data represents a functional called data that was declared in another file. I just want it to be called by using call_user_func that will set the parameter with the function's name and call it within the createtable function, but doesn't seem to work.

I got the example from the PHP Manual, but createTable contains many parameters. How can I make call_user_func only assign the string data to the optional parameter $data set to NULL as default?

function createTable($database, $table, $patch,$data = NULL)
{

    echo "INFO: Adding '$table'in database '$database'.\n";
    $sql = "SHOW TABLES FROM $database WHERE Tables_in_$database='$table';";
    $result = mysql_query($sql);
    $result_count = mysql_num_rows($result);

    if ( $result_count != 1 ) {
          echo "ERROR: Can not find table '$table' in database '$database'.\n";
          $result = mysql_query($patch);
          if ( false === $result ) {
                echo "ERROR: Adding Table '$table' in database '$database' ... Failed\n";
                return false;
            }
          else {
          echo "INFO: Adding Table '$table'in database '$database' ... Success\n";
             // using the optional parameter here 
              $data();
          return true;
               }

    } else {

        if ( $result_count == 1 ) {
            echo "ERROR: Table '$table'already in database '$database'.\n";
            return false;
        }

    }
}

// Now I'm passing value to the optional parameter $ data that is NULL as default.

call_user_func('createTable', "data");

Solution

  • you must pass value like this

    call_user_func('createTable', $database, $table, $patch,$data);
    

    or this for call from class

    call_user_func(array(&$objectName->{$anotherObject},$functionName), $arg1, $arg2, $arg2);
    

    or you can use this can get arg as array

    call_user_func_array("createTable", array("one", "two"));
    

    or this for call from class can get arg as array

    call_user_func_array(array($foo, "bar"), array("three", "four"));
    

    or This can help you too it not need to pass all args

     function __named($method, array $args = array()) 
    { 
    
    $reflection = new ReflectionFunction( $method); 
    
    $pass = array(); 
    foreach($reflection->getParameters() as $param) 
    { 
      /* @var $param ReflectionParameter */ 
      if(isset($args[$param->getName()])) 
      { 
        $pass[] = $args[$param->getName()]; 
      } 
      else 
      { 
        try{
           $pass[] = $param->getDefaultValue();
        }catch(Exception $e){
           $pass[] = NULL; 
        }
      } 
    } 
    
      return $reflection->invokeArgs( $pass); 
     } 
    

    I hope It Work

    sample:

    __named('createTable', array('data' => 'value')); 
    

    and it is for use in class

    public function __named($method, array $args = array()) 
    { 
    
       $reflection = new ReflectionMethod($this, $method); 
    
       $pass = array(); 
       foreach($reflection->getParameters() as $param) 
       { 
          /* @var $param ReflectionParameter */ 
          if(isset($args[$param->getName()])) 
          { 
             $pass[] = $args[$param->getName()]; 
          } 
          else 
          { 
         try{
               $pass[] = $param->getDefaultValue();
             }catch(Exception $e){
                $pass[] = NULL; 
             }
          } 
       } 
    
       return $reflection->invokeArgs($this,$pass); 
    } 
    

    if you Don't set any value __named Put Null instead of Unset Value