phpcms/6194.html">php/227461.html">php之可變函數的實例詳解
php的可變函數,今天大概的了解下,是看php手冊總結的,覺得用處不大;
PHP 支持可變函數的概念。這意味著如果一個變量名后有圓括號,PHP 將尋找與變量的值同名的函數,并且嘗試執行它??勺兒瘮悼梢杂脕韺崿F包括回調函數,函數表在內的一些用途。
可變函數不能用于例如 echo,print,unset(),isset(),empty(),include,require 以及類似的語言結構。需要使用自己的包裝函數來將這些結構用作可變函數。
class Foo{ function Variable() { $name = 'Bar'; $this->$name(); // This calls the Bar() method } function Bar() { echo "This is Bar"; }} $foo = new Foo();$funcname = "Variable";$foo->$funcname(); // This calls $foo->Variable() class Foo{ static $variable = 'static property'; static function Variable() { echo 'Method Variable called'; }} echo Foo::$variable; // This prints 'static property'. It does need a $variable in this scope.$variable = "Variable";Foo::$variable(); // This calls $foo->Variable() reading $variable in this scope.
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答
圖片精選