functions.user-defined.html
10.1 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!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>Vom Nutzer definierte Funktionen</title>
 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.functions.html">Funktionen</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="functions.arguments.html">Funktionsparameter</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.user-defined" class="sect1">
   <h2 class="title">Vom Nutzer definierte Funktionen</h2>
   <p class="para">
    Eine Funktion kann wie folgt definiert werden:
   </p>
   <p class="para">
    <div class="example" id="example-109">
     <p><strong>Beispiel #1 Pseudocode zur Demonstration der Nutzung von Variablen</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">(</span><span style="color: #0000BB">$arg_1</span><span style="color: #007700">, </span><span style="color: #0000BB">$arg_2</span><span style="color: #007700">, ..., </span><span style="color: #0000BB">$arg_n</span><span style="color: #007700">)<br />{<br />    echo </span><span style="color: #DD0000">"Beispielfunktion.\n"</span><span style="color: #007700">;<br />    return </span><span style="color: #0000BB">$retval</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
     </div>
    </div>
   </p>
   <p class="simpara">
    Jeder beliebige korrekte PHP-Code kann in einer Funktion
    vorkommen, sogar andere Funktionen und <a href="keyword.class.html" class="link">Klassen</a>-Definitionen.
   </p>
   <p class="para">
    Für Funktionsnamen gelten in PHP die gleichen Regeln wie für
    andere Bezeichner. Ein gültiger Funktionsname beginnt mit
    einem Buchstaben oder Unterstrich gefolgt von einer beliebigen
    Anzahl von Buchstaben, Ziffern und Unterstrichen. Als
    regulärer Ausdruck wird dies so ausgedrückt:
    <em>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</em>.
   </p>
   <div class="tip"><strong class="tip">Tipp</strong><p class="simpara">Siehe auch <a href="userlandnaming.html" class="xref">Userland Naming Guide</a>.</p></div>
   <p class="simpara">
    Es ist nicht erforderlich, dass Funktionen bereits definiert sein
    müssen, wenn auf sie verviesen wird,
    <em class="emphasis">außer</em> wenn eine Funktion nur bedingt definiert
    wird, wie in den beiden untenstehenden Beispielen gezeigt.
   </p>
   <p class="para">
    Wenn eine Funktion nur unter bestimmten Bedingungen definiert wird,
    muss die Definition dieser Funktion noch <em class="emphasis">vor</em>
    deren Aufruf abgearbeitet werden.
   </p>
   <p class="para">
    <div class="example" id="example-110">
     <p><strong>Beispiel #2 Bedingte Funktionen</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br />$makefoo </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Wir können foo() von hier aus nicht<br />   aufrufen, da sie noch nicht existiert,<br />   aber wir können bar() aufrufen */<br /><br /></span><span style="color: #0000BB">bar</span><span style="color: #007700">();<br /><br />if (</span><span style="color: #0000BB">$makefoo</span><span style="color: #007700">) {<br />  function </span><span style="color: #0000BB">foo </span><span style="color: #007700">()<br />  {<br />    echo </span><span style="color: #DD0000">"Ich existiere nicht, bis mich die Programmausführung erreicht hat.\n"</span><span style="color: #007700">;<br />  }<br />}<br /><br /></span><span style="color: #FF8000">/* Nun können wir foo() sicher aufrufen,<br />   da $makefoo als true ausgewertet wurde */<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$makefoo</span><span style="color: #007700">) </span><span style="color: #0000BB">foo</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">"Ich existiere sofort nach Programmstart.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
     </div>
    </div>
   </p>
   <p class="para">
    <div class="example" id="example-111">
     <p><strong>Beispiel #3 Funktionen innerhalb von Funktionen</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 />  function </span><span style="color: #0000BB">bar</span><span style="color: #007700">()<br />  {<br />    echo </span><span style="color: #DD0000">"Ich existiere nicht, bis foo() aufgerufen wurde.\n"</span><span style="color: #007700">;<br />  }<br />}<br /><br /></span><span style="color: #FF8000">/* Wir können bar() noch nicht<br />   aufrufen, da es nicht existiert */<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/* Nun können wir auch bar() aufrufen,<br />   da sie durch die Abarbeitung von<br />   foo() verfügbar gemacht wurde */<br /><br /></span><span style="color: #0000BB">bar</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
     </div>
    </div>
   </p>
   <p class="para">
    Alle Funktionen und Klassen in PHP existieren im globalen Namensraum -
    sie können außerhalb von Funktionen aufgerufen werden, selbst wenn sie
    innerhalb einer Funktion definiert wurden und umgekehrt.
   </p>
   <p class="simpara">
    PHP unterstützt weder das Überladen von Funktionen, noch ist es
    möglich, zuvor deklarierte Funktionen neu zu definieren oder die
    Definition zu löschen.
   </p>
   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <span class="simpara">
     Groß- und Kleinschreibung spielt zwar bei Funktionsnamen keine
     Rolle, es empfiehlt sich aber trotzdem bei Funktionsaufrufen
     die gleiche Schreibweise wie in der Deklaration zu benutzen.
    </span>
   </p></blockquote>
   <p class="simpara">
    Sowohl <a href="functions.arguments.html#functions.variable-arg-list" class="link">eine
    variable Anzahl Parameter</a> als auch
    <a href="functions.arguments.html#functions.arguments.default" class="link">Vorgabewerte
    für Parameter</a> werden in Funktionen unterstützt.
    Siehe auch die Funktionsreferenzen für
    <span class="function"><a href="function.func-num-args.html" class="function">func_num_args()</a></span>,
    <span class="function"><a href="function.func-get-arg.html" class="function">func_get_arg()</a></span> und
    <span class="function"><a href="function.func-get-args.html" class="function">func_get_args()</a></span> für weitere Informationen.
   </p>
   <p class="para">
    Es ist in PHP möglich, Funktionen rekursiv aufzurufen.
    Sie sollten aber rekursive Aufrufe mit einer Rekursionstiefe
    von mehr als 100-200 vermeiden, da diese zu einem Stacküberlauf
    und damit zum Programmabbruch führen können.
    <div class="example" id="example-112">
     <p><strong>Beispiel #4 Rekursive Funktionen</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">recursion</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)<br />{<br />    if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">< </span><span style="color: #0000BB">20</span><span style="color: #007700">) {<br />        echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">recursion</span><span style="color: #007700">(</span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />    }<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
     </div>
    </div>
   </p>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.functions.html">Funktionen</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="functions.arguments.html">Funktionsparameter</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>