当前位置:首页 > PHP教程 > php函数 > 列表

php之可变函数的实例详解

发布:smiling 来源: PHP粉丝网  添加日期:2018-09-13 16:20:10 浏览: 评论:0 

php的可变函数,今天大概的了解下,是看php手册总结的,觉得用处不大;

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

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

  1. class Foo 
  2.   function Variable() 
  3.   { 
  4.     $name = 'Bar'
  5.     $this->$name(); // This calls the Bar() method 
  6.   } 
  7.    
  8.   function Bar() 
  9.   { 
  10.     echo "This is Bar"
  11.   } 
  12.    
  13. $foo = new Foo(); 
  14. $funcname = "Variable"
  15. $foo->$funcname();  // This calls $foo->Variable() 
  16.    
  17. class Foo 
  18.   static $variable = 'static property'
  19.   static function Variable() 
  20.   { 
  21.     echo 'Method Variable called'
  22.   } 
  23. //phpfensi.com 
  24. echo Foo::$variable// This prints 'static property'. It does need a $variable in this scope. 
  25. $variable = "Variable"
  26. Foo::$variable(); // This calls $foo->Variable() reading $variable in this scope. 

Tags: 函数 实例

分享到: