functions.variable-functions.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 66 行
HTML
66 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Variable functions</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="functions.returning-values.html">Returning values</a></div> <div class="next" style="text-align: right; float: right;"><a href="functions.internal.html">Internal (built-in) functions</a></div> <div class="up"><a href="language.functions.html">Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="functions.variable-functions" class="sect1"> <h2 class="title">Variable functions</h2> <p class="para"> PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth. </p> <p class="para"> Variable functions won't work with language constructs such as <a href="function.echo.html" class="function">echo()</a>, <a href="function.print.html" class="function">print()</a>, <a href="function.unset.html" class="function">unset()</a>, <a href="function.isset.html" class="function">isset()</a>, <a href="function.empty.html" class="function">empty()</a>, <b>include()</b>, <b>require()</b> and the like. Utilize wrapper functions to make use of any of these constructs as variable functions. </p> <p class="para"> <div class="example"> <p><b>Example #1 Variable function example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"In foo()<br />\n"</span><span style="color: #007700">;<br />}<br /><br />function </span><span style="color: #0000BB">bar</span><span style="color: #007700">(</span><span style="color: #0000BB">$arg </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">)<br />{<br /> echo </span><span style="color: #DD0000">"In bar(); argument was '$arg'.<br />\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// This is a wrapper function around echo<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">echoit</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)<br />{<br /> echo </span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$func </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$func</span><span style="color: #007700">(); </span><span style="color: #FF8000">// This calls foo()<br /><br /></span><span style="color: #0000BB">$func </span><span style="color: #007700">= </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$func</span><span style="color: #007700">(</span><span style="color: #DD0000">'test'</span><span style="color: #007700">); </span><span style="color: #FF8000">// This calls bar()<br /><br /></span><span style="color: #0000BB">$func </span><span style="color: #007700">= </span><span style="color: #DD0000">'echoit'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$func</span><span style="color: #007700">(</span><span style="color: #DD0000">'test'</span><span style="color: #007700">); </span><span style="color: #FF8000">// This calls echoit()<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> An object method can also be called with the variable functions syntax. <div class="example"> <p><b>Example #2 Variable method example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Foo<br /></span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">Variable</span><span style="color: #007700">()<br /> {<br /> </span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Bar'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">$name</span><span style="color: #007700">(); </span><span style="color: #FF8000">// This calls the Bar() method<br /> </span><span style="color: #007700">}<br /> <br /> function </span><span style="color: #0000BB">Bar</span><span style="color: #007700">()<br /> {<br /> echo </span><span style="color: #DD0000">"This is Bar"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$funcname </span><span style="color: #007700">= </span><span style="color: #DD0000">"Variable"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$foo</span><span style="color: #007700">-></span><span style="color: #0000BB">$funcname</span><span style="color: #007700">(); </span><span style="color: #FF8000">// This calls $foo->Variable()<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> See also <a href="function.call-user-func.html" class="function">call_user_func()</a>, <a href="language.variables.variable.html" class="link"> variable variables</a> and <a href="function.function-exists.html" class="function">function_exists()</a>. </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="functions.returning-values.html">Returning values</a></div> <div class="next" style="text-align: right; float: right;"><a href="functions.internal.html">Internal (built-in) functions</a></div> <div class="up"><a href="language.functions.html">Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?