Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:06:07+00:00 2026-06-18T09:06:07+00:00

So I’d like to use the call_user_func to pass data to an optional parameter

  • 0

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");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:06:08+00:00Added an answer on June 18, 2026 at 9:06 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.