Resolves the use of func_num_args and func_get_args functions

  • 2020-06-19 09:53:38
  • OfStack

The func_num_args function returns the number of arguments passed to the function, with the syntax int func_num_args (void).
Description: Returns the number of arguments passed to the currently defined function. If this function is called from outside the function definition, func_get_arg() will generate a warning.
func_num_args() can be used to combine func_get_arg() and func_get_args() to allow user-defined functions to accept the list of variable-ES23en parameters. Where func_get_arg() returns the item from the argument list, with the syntax int func_get_arg (int arg_num), and returns the arg_num argument from the argument list defining the function, starting at 0. And calling this function from outside the function definition will generate a warning; It also generates a warning and returns FALSE when arg_num is larger than the actual number of arguments passed by the function.
The difference between the func_get_args() function and the func_get_arg() function is that the func_get_args() function returns an array whose elements are equivalent to the number of parameter columns of the current user-defined function.

When we build the PHP class, we use these three functions flexibly, which can play a very ideal effect. For example, when we create the class linked by PHP and MYSQL, we can write the following code:

<?php 
class mydb{
    private $user;
   private $pass;
   private $host;
   private $db;

public function __construct(){
   $num_args=func_num_args();
   if($num_args>0){
      $args=func_get_args();
       $this->host=$args[0];
       $this->user=$args[1];
       $this->pass=$args[2];
        this->connect();
    }
} 
 ..................... Omit..................... 
?>


Related articles: