PHP可变函数用法

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

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

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

也可以用可变函数的语法来调用一个对象的方法。  

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

?>

  

当调用静态方法时,函数调用要比静态属性优先:

<?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.

?>

  

时间: 2024-08-09 10:31:36

PHP可变函数用法的相关文章

Python3.x基础学习-函数用法

函数用法 函数的参数类型 不可变类型参数:数值型.字符串str.元组tuple.可变类型:列表list,字典dict函数中a不可变,fun(a)内部修改 a 的值,只是修改另一个复制的对象,不会影响 a 本身.1.传递不可变类型参数,不会影响参数本身2.传递可变类型参数,会影响参数本身 # 不可变参数类型a =1 print(id(a)) # 2009628784 def func(a): a= 10 print(a,id(a)) func(a) # 20 2009629392 def func

Oracle 中 decode 函数用法

Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译值1)ELSIF 条件=值2 THEN RETURN(翻译值2) ......ELSIF 条件=值n THEN RETURN(翻译值n)ELSE RETURN(缺省值)END IFdecode(字段或字段的运算,值1,值2,值3) 这个函数运行的结果是,当字段或字段的运算的值等于值1时,该函数返回值

C#字符串的截取函数用法总结

这篇文章主要介绍了C#字符串的截取函数用法,实例总结了substring,Remove,indexOf等函数的用法,并对具体应用进行了实例分析,需要的朋友可以参考下 本文实例总结了C#常用的字符串截取函数用法.分享给大家供大家参考.具体分析如下: 在C#中字符串截取函数包括有substring 函数,Remove 函数,indexOf 函数,它们三个都可以对字符串进行截取操作,下面我们来分别介绍一下. 下面是截取字符串过程中我们必须知道的以下函数:substring 函数.Remove 函数.i

python之函数用法capitalize()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法capitalize() #capitalize() #说明:将字符串的第一个字母变成大写,其他字母变小写. ''' capitalize(...) S.capitalize() -> string Return a copy of the string S with only its first character capitalized. ''' #案例 str='xiaoden

Python成长之路第二篇(1)_数据类型内置函数用法

数据类型内置函数用法int 关于内置方法是非常的多这里呢做了一下总结 (1)__abs__(...)返回x的绝对值 #返回x的绝对值!!!都是双下划线 x.__abs__() <==> abs(x) 例如: #!/usr/bin/python print "abs(-45) : ", abs(-45) print "abs(100.12) : ", abs(100.12) print "abs(119L) : ", abs(119L)

Python成长之路第二篇(2)_列表元组内置函数用法

列表元组内置函数用法list 元组的用法和列表相似就不一一介绍了 1)def append(self, p_object):将值添加到列表的最后 # real signature unknown; restored from __doc__ """ L.append(object) -- append object to end """ pass (2)def count(self, value): 值的出现次数 # real signature

Python成长之路第二篇(3)_字典的置函数用法

字典的置函数用法(字典dict字典中的key不可以重复) class dict(object): """ dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d =

python之函数用法setdefault()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setdefault() #D.get(k,d) #说明:k在D中,则返回 D[K],如果k不在D中,则返回d值 #D.get(k,d), also set D[k]=d if k not in D ''' >>> help(dict.setdefault) Help on built-in function setdefault: setdefault(...) D.set

python之函数用法islower()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法islower() #http://www.runoob.com/python/att-string-islower.html #islower() #说明:检测字符串是否都由小写字母组成 str = "THIS is string example....wow!!!" print str.islower()#False str = "this is string