PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<返回值内部(内建)函数>
Last updated: Mon, 16 Jul 2012

变量函数

PHP 支持变量函数的概念。这意味着如果一个变量名后有圆括号,PHP 将寻找 与变量的值相同的函数,并且将尝试执行它。除了别的事情以外,这个可以被 用于实现回调函数,函数表等等。

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

例子 17-12. 变量函数示例

<?php
function foo()
{
   echo
"In foo()<br>\n";
}

function
bar($arg = '')
{
   echo
"In bar(); argument was '$arg'.<br>\n";
}

// This is a wrapper function around 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()
?>

您还可以利用变量函数的特性来调用一个对象的方法。

例子 17-13. 变量方法范例

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

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

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

?>

请参阅 call_user_func()变量变量function_exists()




<返回值内部(内建)函数>
 Last updated: Mon, 16 Jul 2012
 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://manual.phpv.net/
Last updated: Thu Jul 7 19:13:47 2005 CST