Articles

The month of PHP functions : variables functions

  • Ecrit par Damien Seguy
  • samedi 28 avril 2007
Image pour le titre du contenu

Ce document est aussi disponible en français fr 


With PHP, both variables and functions may be call dynamically. For variables, you must have the name as a string, and add an extra dollar sign. For functions, you simply have to add the parenthesis and the arguments behind the function name.

<?php
 
 
$fonction = 'var_dump';
$fonction('x');
 
 
?>
 
 
string(1) "x"


Note that you may also call PHP native function this way. Though, it exists an exceptino : language structures.

<?php
 
 
$fonction = 'var_dump';
$fonction('x');
 
 
$fonction = 'print';
$fonction('x');
 
 
 
?>
 
 
string(1) "x"
PHP Fatal error:  Call to undefined function <a href="http://www.php.net/print">print()</a> in script.php on line 11
 
 
Fatal error: Call to undefined function <a href="http://www.php.net/print">print()</a> in /script.php on line 11


If you think that my PHP do not support the print() function, you are mistaken. In fact, some PHP functions have a special status : they are called language structures. From a user point of view, they are similar to functions, except when you have to call them dynamically. There are only a few of them, among them empty() and echo().

To call them dynamically, you'll have to create a middle function.
<?php
 
 
function my_print($x) {
    print $x;
}
 
 
$fonction = 'my_print';
$fonction('x');
 
 
?>

Functions may also be call dynamically, thanks to another function : call_user_func(). This function uses the first argument as the name of the function, and then, all the remaining arguments will be the arguments of the actual function.

<?php
 
 
$fonction = 'var_dump';
$fonction('x');
 
 
call_user_func('var_dump', 'x');
 
 
?>

Those two examples are identical, and functions (sic) the same way. The only difference may be that the second version make the dynamical call explicit.

This second function has also a close cousine, with a twist :  call_user_func_array(). The _array suffix indicates that the second argument is an array, containing the arguments. Now, you may pass a dynamical number of arguments to your functions.

<?php
 
 
call_user_func('fonction', range('a','z'));
 
 
function <a href="http://www.php.net/fonction">fonction()</a> {
    $args = <a href="http://www.php.net/func_get_args">func_get_args()</a>;
    
    print_r($args);
}
 
 
?>

This way, you may create function accepting a dynmamical number of arguments, just like array_merge(), for example. By the way, note the use of func_get_args() to get the variable list of arguments as an array.

Keep in mind

  • it is possible to call dynmically a static method, by using an array : the first element will be the class, as a string, and the second element will be the method called. it is possible to call dynmically a method from an objet, by using an array : the first element will be the objet, and the second element will be the method called. From objects, you may also call methods with the dynamic notation. This won't work with static methods, though.
< Précédent   Suivant >

Commentaires

Vous pouvez ajouter votre commentaire!


Vous devez vous connecter pour commenter