## 可變函數
PHP 支持可變函數的概念。這意味著如果一個變量名后有圓括號,PHP 將尋找與變量的值同名的函數,并且嘗試執行它。可變函數可以用來實現包括回調函數,函數表在內的一些用途。
可變函數不能用于例如 [echo](http://php.net/manual/zh/function.echo.php),[print](http://php.net/manual/zh/function.print.php),[unset()](http://php.net/manual/zh/function.unset.php),[isset()](http://php.net/manual/zh/function.isset.php),[empty()](http://php.net/manual/zh/function.empty.php),[include](http://php.net/manual/zh/function.include.php),[require](http://php.net/manual/zh/function.require.php) 以及類似的語言結構。需要使用自己的包裝函數來將這些結構用作可變函數。
**Example #1 可變函數示例**
```
<?php
function?foo()?{
????echo?"In?foo()<br?/>\n";
}
function?bar($arg?=?'')?{
????echo?"In?bar();?argument?was?'$arg'.<br?/>\n";
}
//?使用?echo?的包裝函數
function?echoit($string)
{
????echo?$string;
}
$func?=?'foo';
$func();????????//?This?calls?foo()
$func?=?'bar';
$func('test');??//?This?calls?bar()
$func?=?'echoit';
$func('test');??//?This?calls?echoit()
?>
```
也可以用可變函數的語法來調用一個對象的方法。
**Example #2 可變方法范例**
```
<?php
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()
?>
```
當調用靜態方法時,函數調用要比靜態屬性優先:
**Example #3 Variable 方法和靜態屬性示例**
```
<?php
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.
?>
```
As of PHP 5.4.0, you can call any [callable](http://php.net/manual/zh/language.types.callable.php) stored in a variable.
**Example #4 Complex callables**
```
<?php
class?Foo
{
????static?function?bar()
????{
????????echo?"bar\n";
????}
????function?baz()
????{
????????echo?"baz\n";
????}
}
$func?=?array("Foo",?"bar");
$func();?//?prints?"bar"
$func?=?array(new?Foo,?"baz");
$func();?//?prints?"baz"
$func?=?"Foo::bar";
$func();?//?prints?"bar"?as?of?PHP?7.0.0;?prior,?it?raised?a?fatal?error
?>
```
參見 [is\_callable()](http://php.net/manual/zh/function.is-callable.php),[call\_user\_func()](http://php.net/manual/zh/function.call-user-func.php),[可變變量](http://php.net/manual/zh/language.variables.variable.php)和 [function\_exists()](http://php.net/manual/zh/function.function-exists.php)。
- 序言
- 簡介
- PHP是什么?
- PHP能做什么?
- 基本語法
- 類型
- boolean(布爾型)
- integer(整型)
- float(浮點型)
- string(字符串)
- array(數組)
- object(對象)
- callable(可調用)
- resource(資源)
- NULL(無類型)
- 偽類型
- 類型轉換的判別
- 變量
- 基礎
- 預定義變量
- 變量范圍
- 可變變量
- 來自PHP之外的變量
- 常量
- 語法
- 魔術常量
- 表達式
- 運算符
- 運算符優先級
- 算術運算符
- 賦值運算符
- 位運算符
- 比較運算符
- 錯誤控制運算符
- 執行運算符
- 遞增/遞減運算符
- 邏輯運算符
- 字符串運算符
- 數組運算符
- 類型運算符
- 流程控制
- if
- else
- elseif/else if
- 流程控制的替代語法
- while
- do-whille
- for
- foreach
- break
- continue
- switch
- declare
- return
- require
- include
- require_once
- include_once
- goto
- 函數
- 用戶自定義函數
- 函數的參數
- 返回值
- 可變函數
- 內部 (內置)函數
- 匿名函數
- 類與對象
- 簡介
- 基本概念
- 屬性
- 類的自動加載
- 構造函數
- 訪問控制(可見性)