Question

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");
Was it helpful?

Solution 2

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

OTHER TIPS

Even with call_user_func you have to pass all the parameters.

Anyway, call_user_func is intended for use when the name of the function isn't necessarily known up front. For instance, you might have several functions and a variable, and the variable contains the name of the function to call.

Personally I think it's on par with eval and variable variables: A horrible idea. After all, if you have $foo = "function_name"; then you can call $foo() and it will call function_name.

Anyway, back to the point, just call it as a normal function and give it the parameters it needs. Pass null if you have to.

It seems you just want to pass the last param, and not worry about the 1st three. I don't think call_user_func is the right tool here at all.

Why not just make a function that calls your function?

function call_createTable($data){
    $database = '...';
    $table = '...';
    $patch = '...';

    return createTable($database, $table, $patch, $data);
}

Then just simply call it like this: call_createTable("data");.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top