首頁  >  文章  >  後端開發  >  php之可變函數使用方法詳解

php之可變函數使用方法詳解

黄舟
黄舟原創
2017-09-14 09:23:561439瀏覽

這篇文章主要介紹了php之可變函數的實例詳解的相關資料,希望透過本文能幫助到大家,讓大家理解掌握可變函數,需要的朋友可以參考下

#php之可變函數的實例詳解

php的可變函數,今天大概的了解下,是看php手冊總結的,覺得用處不大;

#PHP 支援可變函數的概念。這意味著如果一個變數名後面有圓括號,PHP 將尋找與變數的值同名的函數,並且嘗試執行它。可變函數可以用來實現包括回調函數,函數表在內的一些用途。

可變函數不能用於例如 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.

以上是php之可變函數使用方法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn