养浩然之气,活着不是为技术,请关心身边的人.
变量函数
上一篇 /
下一篇 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"; }
functionbar($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 }
functionBar() { echo"This is Bar"; } }
$foo= newFoo(); $funcname="Var"; $foo->$funcname(); // This calls $foo->Var()
?> |
|
相关阅读:
- HTML文档中小meta的大作用 (xiaotian_ls, 2007-10-29)
- XHTML代码规范 (xiaotian_ls, 2007-10-29)
- Php魔术函数学习与应用 (xiaotian_ls, 2007-10-30)
- 用魔术方法实现对表单FORM的封装 (xiaotian_ls, 2007-10-30)
- php中的stdclass (xiaotian_ls, 2007-10-30)
- 分页 (xiaotian_ls, 2007-10-30)
- 几大网站使用的操作系统、Web服务器和开发语言 (wanggen0704, 2007-10-31)
- php技术blog开通了 (来影, 2007-10-31)
- IOC详解 (xiaotian_ls, 2007-11-01)
- php中ioc (xiaotian_ls, 2007-11-01)
导入论坛
收藏
分享给好友
管理
举报
TAG:
php