养浩然之气,活着不是为技术,请关心身边的人.

变量函数

上一篇 / 下一篇  2007-11-01 14:27:13 / 个人分类:php

变量函数

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

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

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

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

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

// This is a wrapper function around echo
functionechoit($string)
{
    echo
$string;
}

$func='foo';
$func();        // This calls foo()

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

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

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

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

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

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

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

?>


TAG: php

引用 删除 Guest   /   2008-05-25 22:35:43
www.xingchenbian33.cn
www.meinv880.cn
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

数据统计

  • 访问量: 21680
  • 日志数: 146
  • 建立时间: 2007-08-01
  • 更新时间: 2008-02-20

RSS订阅

Open Toolbar