首页 > 后端开发 > php教程 > PHP的学习-可变函数

PHP的学习-可变函数

WBOY
发布: 2016-06-13 12:27:46
原创
1016 人浏览过

PHP的学习--可变函数

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

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

 

Example #1 可变函数示例

<span style="color: #000000;">php</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> foo() {    </span><span style="color: #0000ff;">echo</span> "In foo()<br>\n"<span style="color: #000000;">;}</span><span style="color: #0000ff;">function</span> bar(<span style="color: #800080;">$arg</span> = ''<span style="color: #000000;">) {    </span><span style="color: #0000ff;">echo</span> "In bar(); argument was '<span style="color: #800080;">$arg</span>'.<br>\n"<span style="color: #000000;">;}</span><span style="color: #008000;">//</span><span style="color: #008000;"> 使用 echo 的包装函数</span><span style="color: #0000ff;">function</span> echoit(<span style="color: #800080;">$string</span><span style="color: #000000;">){    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$string</span><span style="color: #000000;">;}</span><span style="color: #800080;">$func</span> = 'foo'<span style="color: #000000;">;</span><span style="color: #800080;">$func</span>();        <span style="color: #008000;">//</span><span style="color: #008000;"> This calls foo()</span><span style="color: #800080;">$func</span> = 'bar'<span style="color: #000000;">;</span><span style="color: #800080;">$func</span>('test');  <span style="color: #008000;">//</span><span style="color: #008000;"> This calls bar()</span><span style="color: #800080;">$func</span> = 'echoit'<span style="color: #000000;">;</span><span style="color: #800080;">$func</span>('test');  <span style="color: #008000;">//</span><span style="color: #008000;"> This calls echoit()</span>?>
登录后复制

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

<span style="color: #000000;">php</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Foo{    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> Variable()    {        </span><span style="color: #800080;">$name</span> = 'Bar'<span style="color: #000000;">;        </span><span style="color: #800080;">$this</span>-><span style="color: #800080;">$name</span>(); <span style="color: #008000;">//</span><span style="color: #008000;"> This calls the Bar() method</span><span style="color: #000000;">    }    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> Bar()    {        </span><span style="color: #0000ff;">echo</span> "This is Bar"<span style="color: #000000;">;    }}</span><span style="color: #800080;">$foo</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Foo();</span><span style="color: #800080;">$funcname</span> = "Variable"<span style="color: #000000;">;</span><span style="color: #800080;">$foo</span>-><span style="color: #800080;">$funcname</span>();   <span style="color: #008000;">//</span><span style="color: #008000;"> This calls $foo->Variable()</span>?>
登录后复制

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

Example #3 Variable 方法和静态属性示例

<span style="color: #000000;">php</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Foo{    </span><span style="color: #0000ff;">static</span> <span style="color: #800080;">$variable</span> = 'static property'<span style="color: #000000;">;    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> Variable()    {        </span><span style="color: #0000ff;">echo</span> 'Method Variable called'<span style="color: #000000;">;    }}</span><span style="color: #0000ff;">echo</span> Foo::<span style="color: #800080;">$variable</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> This prints 'static property'. It does need a $variable in this scope.</span><span style="color: #800080;">$variable</span> = "Variable"<span style="color: #000000;">;Foo</span>::<span style="color: #800080;">$variable</span>();  <span style="color: #008000;">//</span><span style="color: #008000;"> This calls $foo->Variable() reading $variable in this scope.</span>?>
登录后复制

可以使用可变函数的方法列表如下:

<span style="color: #000000;">php</span><span style="color: #008080;">array_diff_assoc</span><span style="color: #000000;">()array_diff_key()</span><span style="color: #008080;">array_diff_uassoc</span><span style="color: #000000;">()</span><span style="color: #0000ff;">array</span><span style="color: #000000;">()array_intersect_ukey()</span><span style="color: #008080;">array_map</span><span style="color: #000000;">()</span><span style="color: #008080;">array_merge</span><span style="color: #000000;">()</span><span style="color: #008080;">array_merge_recursive</span><span style="color: #000000;">()</span><span style="color: #008080;">array_multisort</span><span style="color: #000000;">()</span><span style="color: #008080;">array_push</span><span style="color: #000000;">()array_replace()array_replace_recursive()</span><span style="color: #008080;">array_unshift</span><span style="color: #000000;">()</span><span style="color: #008080;">call_user_func</span><span style="color: #000000;">()</span><span style="color: #008080;">call_user_method</span><span style="color: #000000;">()</span><span style="color: #008080;">compact</span><span style="color: #000000;">()dba_open()dba_popen()</span><span style="color: #0000ff;">echo</span><span style="color: #000000;">()forward_static_call()</span><span style="color: #008080;">fprintf</span><span style="color: #000000;">()</span><span style="color: #008080;">fscanf</span><span style="color: #000000;">()httprequestpool_construct()ibase_execute()ibase_set_event_handler()ibase_wait_event()</span><span style="color: #0000ff;">isset</span><span style="color: #000000;">()</span><span style="color: #0000ff;">list</span><span style="color: #000000;">()maxdb_stmt_bind_param()maxdb_stmt_bind_result()mb_convert_variables()newt_checkbox_tree_add_item()newt_grid_h_close_stacked()newt_grid_h_stacked()newt_grid_v_close_stacked()newt_grid_v_stacked()newt_win_choice()newt_win_entries()newt_win_menu()newt_win_message()newt_win_ternary()</span><span style="color: #008080;">pack</span><span style="color: #000000;">()</span><span style="color: #008080;">printf</span><span style="color: #000000;">()</span><span style="color: #008080;">register_shutdown_function</span><span style="color: #000000;">()</span><span style="color: #008080;">register_tick_function</span><span style="color: #000000;">()</span><span style="color: #008080;">session_register</span><span style="color: #000000;">()</span><span style="color: #008080;">setlocale</span><span style="color: #000000;">()</span><span style="color: #008080;">sprintf</span><span style="color: #000000;">()</span><span style="color: #008080;">sscanf</span><span style="color: #000000;">()</span><span style="color: #0000ff;">unset</span><span style="color: #000000;">()</span><span style="color: #008080;">var_dump</span><span style="color: #000000;">()w32api_deftype()w32api_init_dtype()w32api_invoke_function()</span><span style="color: #008080;">wddx_add_vars</span><span style="color: #000000;">()</span><span style="color: #008080;">wddx_serialize_vars</span>()
登录后复制

 

摘自:http://php.net/manual/zh/functions.variable-functions.php

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板