functions.variable-functions.html
7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Variablenfunktionen</title>
</head>
<body><div class="manualnavbar" style="text-align: center;">
<div class="prev" style="text-align: left; float: left;"><a href="functions.returning-values.html">Rückgabewerte</a></div>
<div class="next" style="text-align: right; float: right;"><a href="functions.internal.html">Interne (eingebaute) Funktionen</a></div>
<div class="up"><a href="language.functions.html">Funktionen</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">Variablenfunktionen</h2>
<p class="para">
PHP unterstützt das Konzept der Variablenfunktionen. Wenn Sie an
das Ende einer Variablen Klammern hängen, versucht PHP eine
Funktion aufzurufen, deren Name der aktuelle Wert der Variablen
ist. Dies kann unter anderem für Callbacks, Funktionstabellen,
usw. genutzt werden.
</p>
<p class="para">
Variablenfunktionen funktionieren nicht mit Sprachkonstrukten
wie <span class="function"><a href="function.echo.html" class="function">echo</a></span>, <span class="function"><a href="function.print.html" class="function">print</a></span>,
<span class="function"><a href="function.unset.html" class="function">unset()</a></span>, <span class="function"><a href="function.isset.html" class="function">isset()</a></span>,
<span class="function"><a href="function.empty.html" class="function">empty()</a></span>, <span class="function"><a href="function.include.html" class="function">include</a></span> und
<span class="function"><a href="function.require.html" class="function">require</a></span>. Sie müssen Ihre eigenen Wrapperfunktionen
verwenden, um diese Konstrukte als variable Funktionen benutzen zu
können.
</p>
<p class="para">
<div class="example" id="example-126">
<p><strong>Beispiel #1 Beispiel für Variablenfunktionen</strong></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 />{<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(); der Parameter ist '</span><span style="color: #0000BB">$arg</span><span style="color: #DD0000">'.<br />\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Dies ist eine Wrapperfunkiton für 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">// Dies ruft foo() auf<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">// Dies ruft bar() auf<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">// Dies ruft echoit() auf<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div>
</p>
<p class="para">
Sie können auch die Methode eines Objektes mittels der variablen
Funktionen aufrufen.
<div class="example" id="example-127">
<p><strong>Beispiel #2 Variable Methode</strong></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">// Dies ruft die Bar() Methode auf<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">"Das ist 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">// Dies ruft $foo->Variable() auf<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div>
</p>
<p class="para">
Siehe auch
<span class="function"><a href="function.call-user-func.html" class="function">call_user_func()</a></span>,
<a href="language.variables.variable.html" class="link">Variable Variablen</a> und
<span class="function"><a href="function.function-exists.html" class="function">function_exists()</a></span>.
</p>
</div><hr /><div class="manualnavbar" style="text-align: center;">
<div class="prev" style="text-align: left; float: left;"><a href="functions.returning-values.html">Rückgabewerte</a></div>
<div class="next" style="text-align: right; float: right;"><a href="functions.internal.html">Interne (eingebaute) Funktionen</a></div>
<div class="up"><a href="language.functions.html">Funktionen</a></div>
<div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>