Обращение к функциям через переменные
PHP поддерживает концепцию переменных функций. Это означает, что если к имени переменной присоединены круглые скобки, PHP ищет функцию с тем же именем, что и результат вычисления переменной, и пытается ее выполнить. Эту возможность можно использовать для реализации обратных вызовов, таблиц функций и множества других вещей.
Переменные функции не будут работать с такими языковыми конструкциями как echo(), print(), unset(), isset(), empty(), include(), require() и другими подобными им операторами. Вам необходимо реализовывать свою функцию-обертку (wrapper) для того, чтобы приведенные выше конструкции могли работать с переменными функциями.
Пример #1 Работа с функциями посредством переменных
<?php
function foo() {
echo "In foo()<br />\n";
}
function bar($arg = '')
{
echo "In bar(); argument was '$arg'.<br />\n";
}
// Функция-обертка для echo
function echoit($string)
{
echo $string;
}
$func = 'foo';
$func(); // Вызывает функцию foo()
$func = 'bar';
$func('test'); // Вызывает функцию bar()
$func = 'echoit';
$func('test'); // Вызыывет функцию echoit()
?>
Вы также можете вызвать методы объекта, используя возможности PHP для работы с переменными функциями.
Пример #2 Обращение к методам класса посредством переменных
<?php
class Foo
{
function Variable()
{
$name = 'Bar';
$this->$name(); // Вызываем метод Bar()
}
function Bar()
{
echo "This is Bar";
}
}
$foo = new Foo();
$funcname = "Variable";
$foo->$funcname(); // Обращаемся к $foo->Variable()
?>
Смотрите также call_user_func(), Переменные переменные и function_exists().
Коментарии
If you want to call a static function (PHP5) in a variable method:
Make an array of two entries where the 0th entry is the name of the class to be invoked ('self' and 'parent' work as well) and the 1st entry is the name of the function. Basically, a 'callback' variable is either a string (the name of the function) or an array (0 => 'className', 1 => 'functionName').
Then, to call that function, you can use either call_user_func() or call_user_func_array(). Examples:
<?php
class A {
protected $a;
protected $c;
function __construct() {
$this->a = array('self', 'a');
$this->c = array('self', 'c');
}
static function a($name, &$value) {
echo $name,' => ',$value++,"\n";
}
function b($name, &$value) {
call_user_func_array($this->a, array($name, &$value));
}
static function c($str) {
echo $str,"\n";
}
function d() {
call_user_func_array($this->c, func_get_args());
}
function e() {
call_user_func($this->c, func_get_arg(0));
}
}
class B extends A {
function __construct() {
$this->a = array('parent', 'a');
$this->c = array('self', 'c');
}
static function c() {
print_r(func_get_args());
}
function d() {
call_user_func_array($this->c, func_get_args());
}
function e() {
call_user_func($this->c, func_get_args());
}
}
$a =& new A;
$b =& new B;
$i = 0;
A::a('index', $i);
$a->b('index', $i);
$a->c('string');
$a->d('string');
$a->e('string');
# etc.
?>
$ wget http://www.php.net/get/php_manual_en.tar.gz/from/a/mirror
$ grep -l "\$\.\.\." php-chunked-xhtml/function.*.html
List of functions that accept variable arguments.
<?php
array_diff_assoc()
array_diff_key()
array_diff_uassoc()
array()
array_intersect_ukey()
array_map()
array_merge()
array_merge_recursive()
array_multisort()
array_push()
array_replace()
array_replace_recursive()
array_unshift()
call_user_func()
call_user_method()
compact()
dba_open()
dba_popen()
echo()
forward_static_call()
fprintf()
fscanf()
httprequestpool_construct()
ibase_execute()
ibase_set_event_handler()
ibase_wait_event()
isset()
list()
maxdb_stmt_bind_param()
maxdb_stmt_bind_result()
mb_convert_variables()
newt_checkbox_tree_add_item()
newt_grid_h_close_stacked()
newt_grid_h_stacked()
newt_grid_v_close_stacked()
newt_grid_v_stacked()
newt_win_choice()
newt_win_entries()
newt_win_menu()
newt_win_message()
newt_win_ternary()
pack()
printf()
register_shutdown_function()
register_tick_function()
session_register()
setlocale()
sprintf()
sscanf()
unset()
var_dump()
w32api_deftype()
w32api_init_dtype()
w32api_invoke_function()
wddx_add_vars()
wddx_serialize_vars()
?>
A small, but helpful note. If you are trying to call a static function from a different namespace, you must use the fully qualified namespace, even if they have the same top level namespace(s). For example if you have the following class to call:
<?php
namespace ProjectTestClass;
class Test {
static function funcToCall() {
return "test";
}
}
?>
You must call it as:
<?php
namespace ProjectOtherTestClass;
class OtherTest {
static function callOtherFunc() {
$func = '\Project\TestClass::funcToCall';
$func();
}
}
?>
and not:
<?php
class OtherTest {
static function callOtherFunc() {
$func = 'TestClass::funcToCall';
$func();
}
}
?>