php 什么是动态函数,PHP 动态函数调用(转)

Variable functions

PHP supports the concept of variable functions.

This means that if a variable name has parentheses appended to it,

PHP will look for a function with the same name as whatever the

variable evaluates to, and will attempt to execute it. Among other

things, this can be used to implement callbacks, function tables,

and so forth.

Variable functions won't work with language

constructs such as

Example #1 Variable function example

echo"In foo()
\n";

}

functionbar($arg='')

{

echo"In bar(); argument was '$arg'.
\n";

}// This is a wrapper function around echofunctionechoit($string)

{

echo$string;

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

An object method can also be called with the

variable functions syntax.

Example #2 Variable method example

functionVariable()

{$name='Bar';$this->$name();// This calls the Bar() method}

functionBar()

{

echo"This is Bar";

}

}$foo= newFoo();$funcname="Variable";$foo->$funcname();// This calls $foo->Variable()?>

When calling static methods, the function call is

stronger than the static property operator:

Example #3 Variable method example with static

properties

static$variable='static property';

static functionVariable()

{

echo'Method Variable called';

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值