PHP call_user_func vs. just calling function

Je suis sûr qu'il y a une explication très simple pour cela. Quelle est la différence entre ceci:

function barber($type){ echo "You wanted a $type haircut, no problem\n"; } call_user_func('barber', "mushroom"); call_user_func('barber', "shave"); 

… et ceci (et quels sont les avantages?):

 function barber($type){ echo "You wanted a $type haircut, no problem\n"; } barber('mushroom'); barber('shave'); fonction coiffeur ($ type) { function barber($type){ echo "You wanted a $type haircut, no problem\n"; } barber('mushroom'); barber('shave'); echo "Vous vouliez une coupe de cheveux $ type, pas de problème \ n"; function barber($type){ echo "You wanted a $type haircut, no problem\n"; } barber('mushroom'); barber('shave'); barbier ('champignon'); function barber($type){ echo "You wanted a $type haircut, no problem\n"; } barber('mushroom'); barber('shave'); 

Merci d'avance.

Toujours utiliser le nom de la fonction réelle lorsque vous le savez.

call_user_func est destiné à appeler des fonctions dont vous ne connaissez pas le nom, mais il est beaucoup less efficace car le programme doit searchr la fonction au moment de l'exécution.

Bien que vous puissiez appeler des noms de fonctions variables de cette façon:

 function printIt($str) { print($str); } $funcname = 'printIt'; $funcname('Hello world!'); function printIt ($ str) {print ($ str); function printIt($str) { print($str); } $funcname = 'printIt'; $funcname('Hello world!'); $ funcname = 'printIt'; function printIt($str) { print($str); } $funcname = 'printIt'; $funcname('Hello world!'); 

il y a des cas où vous ne savez pas combien d'arguments vous passez. Considérer ce qui suit:

 function someFunc() { $args = func_get_args(); // do something } call_user_func_array('someFunc',array('one','two','three')); function someFunc () { function someFunc() { $args = func_get_args(); // do something } call_user_func_array('someFunc',array('one','two','three')); $ args = func_get_args (); function someFunc() { $args = func_get_args(); // do something } call_user_func_array('someFunc',array('one','two','three')); 

Il est également pratique pour appeler les methods statiques et object, respectivement:

 call_user_func(array('someClass','someFunc'),$arg); call_user_func(array($myObj,'someFunc'),$arg); call_user_func (array ('someClass', 'someFunc'), $ arg); call_user_func(array('someClass','someFunc'),$arg); call_user_func(array($myObj,'someFunc'),$arg); 

l'option call_user_func est là afin que vous puissiez faire des choses comme:

 $dynamicFunctionName = "barber"; call_user_func($dynamicFunctionName, 'mushroom'); $ dynamicFunctionName = "barbier"; $dynamicFunctionName = "barber"; call_user_func($dynamicFunctionName, 'mushroom'); 

où la string dynamicFunctionName pourrait être plus excitante et générée au cours de l'exécution. Vous ne devez pas utiliser call_user_func à less d'avoir à le faire, car il est plus lent.

J'imagine qu'il est utile d'appeler une fonction que vous ne connaissez pas le nom d'avance … Quelque chose comme:

 switch($value): { case 7: $func = 'run'; break; default: $func = 'stop'; break; } call_user_func($func, 'stuff'); commutateur (valeur $): switch($value): { case 7: $func = 'run'; break; default: $func = 'stop'; break; } call_user_func($func, 'stuff'); cas 7: switch($value): { case 7: $func = 'run'; break; default: $func = 'stop'; break; } call_user_func($func, 'stuff'); $ func = 'run'; switch($value): { case 7: $func = 'run'; break; default: $func = 'stop'; break; } call_user_func($func, 'stuff'); $ func = 'stop'; switch($value): { case 7: $func = 'run'; break; default: $func = 'stop'; break; } call_user_func($func, 'stuff'); 

Il n'y a pas de bénéfices appelant la fonction comme ça parce que je pense qu'il était principalement utilisé pour appeler la fonction "user" (comme un plugin) car l'édition du file de base n'est pas une bonne option. Voici un mauvais exemple utilisé par WordPress

 <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> <? php <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> / * <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> * my_plugin.php <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> * / <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> function myLocation ($ content) { <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> returnner str_replace ('@', 'world', $ content); <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> function myName ($ content) { <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> returnner le contenu $. "Tasikmalaya"; <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> add_filter ('the_content', 'myLocation'); <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> add_filter ('the_content', 'myName'); <?php /* * my_plugin.php */ function myLocation($content){ return str_replace('@', 'world', $content); } function myName($content){ return $content."Tasikmalaya"; } add_filter('the_content', 'myLocation'); add_filter('the_content', 'myName'); ?> 

 <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> <? php <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> / * <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> * core.php <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> * / <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> $ content = "hello @ my name is"; <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> $ listFunc = array (); <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> // stocker la fonction user sur le tableau (dans my_plugin.php) <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> function add_filter ($ fName, $ funct) <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> $ listFunc [$ fName] = $ funct; <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> // exécute la fonction définie par l'user <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> function apply_filter ($ funct, $ content) <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> Global $ listFunc; <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> si (isset ($ listFunc)) <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> foreach ($ listFunc comme $ key => $ value) <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> si ($ key == $ funct) <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> $ content = call_user_func ($ listFunc [$ key], $ content); <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> returnner le contenu $; <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> function the_content () <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> $ content = apply_filter ('the_content', $ content); <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> echo $ content; <?php /* * core.php * read only */ $content = "hello @ my name is "; $listFunc = array(); // store user function to array (in my_plugin.php) function add_filter($fName, $funct) { $listFunc[$fName]= $funct; } // execute list user defined function function apply_filter($funct, $content) { global $listFunc; if(isset($listFunc)) { foreach($listFunc as $key => $value) { if($key == $funct) { $content = call_user_func($listFunc[$key], $content); } } } return $content; } function the_content() { $content = apply_filter('the_content', $content); echo $content; } ?> 

….

 <?php require_once("core.php"); require_once("my_plugin.php"); the_content(); // hello world my name is Tasikmalaya ?> <? php <?php require_once("core.php"); require_once("my_plugin.php"); the_content(); // hello world my name is Tasikmalaya ?> require_once ("core.php"); <?php require_once("core.php"); require_once("my_plugin.php"); the_content(); // hello world my name is Tasikmalaya ?> require_once ("my_plugin.php"); <?php require_once("core.php"); require_once("my_plugin.php"); the_content(); // hello world my name is Tasikmalaya ?> // hello world mon nom est Tasikmalaya <?php require_once("core.php"); require_once("my_plugin.php"); the_content(); // hello world my name is Tasikmalaya ?> 

sortie

 hello world my name is Tasikmalaya 

Dans votre premier exemple, vous utilisez le nom de la fonction qui est une string. Il pourrait venir de l'extérieur ou être déterminé à la volée. c'est-à-dire que vous ne savez pas quelle fonction devra être exécutée au moment de la création du code.

Lorsque vous utilisez des namespaces, call_user_func () est le seul moyen d'exécuter une fonction dont vous ne connaissez pas le nom, par exemple:

 $function = '\Utilities\SearchTools::getCurrency'; call_user_func($function,'USA'); $ function = '\ Utilities \ SearchTools :: getCurrency'; $function = '\Utilities\SearchTools::getCurrency'; call_user_func($function,'USA'); 

Si toutes vos fonctions se trouvaient dans le même espace de noms, ce ne serait pas un problème, car vous pourriez utiliser quelque chose comme ceci:

 $function = 'getCurrency'; $function('USA'); $ function = 'getCurrency'; $function = 'getCurrency'; $function('USA'); 

Edit: Après @Jannis disant que je me trompe, j'ai fait un peu plus de test et je n'avais pas beaucoup de chance:

 <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } <? php <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } namespace foo { <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } class Bar { <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } fonction statique publique getBar () { <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } returnner 'Bar'; <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } echo "<h1> Bar:". \ foo \ Bar :: getBar (). "</ h1>"; <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } // Sorties 'Bar: Bar' <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } $ function = '\ foo \ Bar :: getBar'; <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } echo "<h1> Bar:". $ function (). "</ h1>"; <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } // sorties 'Erreur fatale: appel à la fonction indéfinie \ foo \ Bar :: getBar ()' <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } $ function = '\ foo \ Bar \ getBar'; <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } echo "<h1> Bar:". $ function (). "</ h1>"; <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' } // sort 'Erreur fatale: appel à une fonction indéfinie \ foo \ Bar \ getBar ()' <?php namespace foo { class Bar { public static function getBar() { return 'Bar'; } } echo "<h1>Bar: ".\foo\Bar::getBar()."</h1>"; // outputs 'Bar: Bar' $function = '\foo\Bar::getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar::getBar()' $function = '\foo\Bar\getBar'; echo "<h1>Bar: ".$function()."</h1>"; // outputs 'Fatal error: Call to undefined function \foo\Bar\getBar()' }