hi all,
I cannot understand for what reason and how is used call_user_func_array
Can anyone explain me in a very simple manner what is call_user_func_array? and with examples if possible
Thanks in advance

Recommended Answers

All 3 Replies

thank you very much. but what advantages does this function have? if possible can you explain it in this example
and for what reason here used & operator

while ( $field = $meta->fetch_field() ) {

     $parameters[] = &$row[$field->name];
   }

?

<?php

function read()
{
   $parameters = array();
   $results = array();

   $mysql = new mysqli('localhost', 'root', 'root', 'db') or die('There was a problem connecting to the database');
   $stmt = $mysql->prepare('SELECT body FROM posts') or die('Problem preparing query');
   $stmt->execute();

   $meta = $stmt->result_metadata();

   while ( $field = $meta->fetch_field() ) {

     $parameters[] = &$row[$field->name];
   }

   call_user_func_array(array($stmt, 'bind_result'), $parameters);

   while ( $stmt->fetch() ) {
      $x = array();
      foreach( $row as $key => $val ) {
         $x[$key] = $val;
      }
      $results[] = $x;
   }

   return $results;

}

$results = read();
?>
<!DOCTYPE html>

<html lang="en">
<head>
   <meta charset="utf-8">
   <title>untitled</title>
</head>
<body>
<?php foreach ($results as $row) : ?>

   <p> <?php echo $row['body']; ?> </p>
<?php endforeach; ?>
</body>
</html>

thanks for attention

I realy do not know enough about php coding to give you an educated answer, but what I do know, is that call_user_func_array is useful when the number of parameters to a called function is variable or unknown, and other advantages is it also supports callbacks, so you can easily give a class function or object method to it.

For examples, see this page:

http://bytes.com/topic/php/answers/686312-call_user_func_array-vs-myfunction-params

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.