Home > Backend Development > PHP Tutorial > PHP的学习-可变函数

PHP的学习-可变函数

WBOY
Release: 2016-06-13 12:27:46
Original
1016 people have browsed it

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>?>
Copy after login

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

<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>?>
Copy after login

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

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>?>
Copy after login

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

<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>()
Copy after login

 

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

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template