Quando si utilizzano gli spazi dei nomi, call_user_func () è l'unico modo per eseguire una funzione di cui non si conosce il nome in anticipo, ad esempio:
$function = '\Utilities\SearchTools::getCurrency';
call_user_func($function,'USA');
Se tutte le tue funzioni fossero nello stesso spazio dei nomi, non sarebbe un problema del genere, poiché potresti usare qualcosa del genere:
$function = 'getCurrency';
$function('USA');
Modifica: seguendo @Jannis dicendo che mi sbaglio ho fatto un po 'più di test e non ho avuto molta fortuna:
<?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()'
}
Puoi vedere i risultati dell'output qui: https://3v4l.org/iBERh sembra che il secondo metodo funzioni per PHP 7 in poi, ma non PHP 5.6.