欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP可變函數(shù)的使用詳解

 更新時間:2013年06月14日 16:14:49   作者:  
本篇文章是對PHP中可變函數(shù)的使用進行了詳細的分析介紹,需要的朋友參考下
PHP 支持可變函數(shù)的概念。這意味著如果一個變量名后有圓括號,PHP 將尋找與變量的值同名的函數(shù),并且嘗試執(zhí)行它??勺兒瘮?shù)可以用來實現(xiàn)包括回調(diào)函數(shù),函數(shù)表在內(nèi)的一些用途。
變量函數(shù)不能用于語言結(jié)構(gòu),例如 echo() ,print() ,unset() ,isset() ,empty() ,include() ,require() 以及類似的語句。需要使用自己的包裝函數(shù)來將這些結(jié)構(gòu)用作變量函數(shù)。 
Example #1 可變函數(shù)示例
復(fù)制代碼 代碼如下:

<?php
function  foo () {
    echo  "In foo()<br />/n" ;
}
function  bar ( $arg  =  '' ) {
    echo  "In bar(); argument was ' $arg '.<br />/n" ;
}
// 使用 echo 的包裝函數(shù)
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()
?>
還可以利用可變函數(shù)的特性來調(diào)用一個對象的方法。

Example #2 可變方法范例
復(fù)制代碼 代碼如下:

<?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()
?>

相關(guān)文章

最新評論