文字

可变函数

PHP 支持可变函数的概念。这意味着如果一个变量名后有圆括号,PHP 将寻找与变量的值同名的函数,并且尝试执行它。可变函数可以用来实现包括回调函数,函数表在内的一些用途。

可变函数不能用于例如 echo print unset() isset() empty() include require 以及类似的语言结构。需要使用自己的包装函数来将这些结构用作可变函数。

Example #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 ();         // This calls foo()

$func  'bar' ;
$func ( 'test' );   // This calls bar()

$func  'echoit' ;
$func ( 'test' );   // This calls echoit()
?>

也可以用可变函数的语法来调用一个对象的方法。

Example #2 可变方法范例

<?php
class  Foo
{
    function 
Variable ()
    {
        
$name  'Bar' ;
        
$this -> $name ();  // This calls the Bar() method
    
}

    function 
Bar ()
    {
        echo 
"This is Bar" ;
    }
}

$foo  = new  Foo ();
$funcname  "Variable" ;
$foo -> $funcname ();    // This calls $foo->Variable()

?>

当调用静态方法时,函数调用要比静态属性优先:

Example #3 Variable 方法和静态属性示例

<?php
class  Foo
{
    static 
$variable  'static property' ;
    static function 
Variable ()
    {
        echo 
'Method Variable called' ;
    }
}

echo 
Foo :: $variable // This prints 'static property'. It does need a $variable in this scope.
$variable  "Variable" ;
Foo :: $variable ();   // This calls $foo->Variable() reading $variable in this scope.

?>

参见 is_callable() call_user_func() ,可变变量和 function_exists()

用户评论:

[#1] Anonymous [2015-09-28 11:18:32]

i'm not sure, but simple mistake in this place ($f instead $func):
<?php
$func 
= array("Foo""bar");
$func(); // prints "bar"
$f = array(new Foo"baz");
$func(); // prints "baz"
$f "Foo::bar";
$func(); // prints "bar" as of PHP 7.0.0; prior, it raised a fatal error
?>

[#2] Anonymous [2011-06-27 11:20:48]

$ 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()
?>

[#3] imurnane at internode on net [2011-05-02 03:05:23]

Create and call a dynamically named function

<?php
$tmp 
"foo";
$
$tmp = function() {
    global 
$tmp;
    echo 
$tmp;
}; 

$
$tmp();
?>


Outputs "foo"

[#4] AnonymousPoster at disposeamail dot com [2010-05-03 14:20:54]

Variable functions allows higher-order programming.

Here is the classical map example.

<?php

function map($fun$list,$params=array()){
    
$acc=NULL;
    
$last=array_push($paramsNULL,$acc)-1// alloc $element and $acc at the end
    
foreach($list as $params[$last-1]){
        
$params[$last]=call_user_func_array($fun $params  );
    }
    
$acc=array_pop($params);
    return 
$acc;
}

function 
add($element,$acc){ // maybe only with multi-length function
    
if ($acc == NULL);
    return 
$acc=$element+$acc;
}

$result=0;
$result=addTo($result,1);
$result=addTo($result,2);
$result=addTo($result,3);
echo 
"result = $result\n";

$result=0;
$result=map('addTo',array(1,2,3));
echo 
"result= $result\n";
?>

[#5] boards at gmail dot com [2006-01-22 10:07:14]

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 {

  protected 
$a;
  protected 
$c;

  function 
__construct() {
    
$this->= array('self''a');
    
$this->= 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->cfunc_get_args());
  }

  function 
e() {
    
call_user_func($this->cfunc_get_arg(0));
  }

}

class 
extends {

  function 
__construct() {
    
$this->= array('parent''a');
    
$this->= array('self''c');
  }

  static function 
c() {
    
print_r(func_get_args());
  }

  function 
d() {
    
call_user_func_array($this->cfunc_get_args());
  }

  function 
e() {
    
call_user_func($this->cfunc_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.
?>

[#6] Storm [2005-05-03 08:34:47]

This can quite useful for a dynamic database class:

(Note: This just a simplified section)

<?php
class db {

    private 
$host 'localhost';
    private 
$user 'username';
    private 
$pass 'password';
    private 
$type 'mysqli';
    
    public 
$lid 0;

    
// Connection function
    
function connect() {
        
$connect $this->type.'_connect';
            
        if (!
$this->lid $connect($this->host$this->user$this->pass)) {
            die(
'Unable to connect.');
        }
 }
}
$db  = new db;
$db->connect();
?>


Much easier than having multiple database classes or even extending a base class.

[#7] ian at NO_SPAM dot verteron dot net [2002-12-20 07:33:25]

A good method to pass around variables containing function names within some class is to use the same method as the developers use in preg_replace_callback - with arrays containing an instance of the class and the function name itself.

function call_within_an_object($fun)
{
  if(is_array($fun))
  {
    
    $fun[0]->{$fun[1]}();
  }
  else
  {
    
    $fun();
  }
}

function some_other_fun()
{
  
}

class x
{
  function fun($value)
  {
    
  }
}

$x = new x();


call_within_an_object(Array($x, 'fun'));


call_within_an_object('some_other_fun');

[#8] madeinlisboa at yahoo dot com [2002-09-05 05:14:05]

Finally, a very easy way to call a variable method in a class:

Example of a class:

class Print() {
    var $mPrintFunction;

    function Print($where_to) {
        $this->mPrintFunction = "PrintTo$where_to";
    }

    function PrintToScreen($content) {
        echo $content;
    }

    function PrintToFile($content) {
        fputs ($file, $contents);
    }

.. .. ..

    // first, function name is parsed, then function is called
    $this->{$this->mPrintFunction}("something to print");
}

[#9] msmith at pmcc dot com [2002-05-02 16:49:39]

Try the call_user_func() function.  I find it's a bit simpler to implement, and at very least makes your code a bit more readable... much more readable and simpler to research for someone who isn't familiar with this construct.

上一篇: 下一篇: