functions.anonymous.html
14.3 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
<!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>Anonymous functions</title>
 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="functions.internal.html">Interne (eingebaute) Funktionen</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.oop5.html">Klassen und Objekte</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.anonymous" class="sect1">
   <h2 class="title">Anonymous functions</h2>
   <p class="simpara">
    Anonymous functions, also known as <em>closures</em>, allow the
    creation of functions which have no specified name. They are most useful as
    the value of <a href="language.pseudo-types.html#language.types.callback" class="link">callback</a>
    parameters, but they have many other uses.
   </p>
   <div class="example" id="example-128">
    <p><strong>Beispiel #1 Anonymous function example</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">echo </span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(</span><span style="color: #DD0000">'~-([a-z])~'</span><span style="color: #007700">, function (</span><span style="color: #0000BB">$match</span><span style="color: #007700">) {<br />    return </span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$match</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br />}, </span><span style="color: #DD0000">'hello-world'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// outputs helloWorld<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
    </div>
   </div>
   <p class="simpara">
    Closures can also be used as the values of variables; PHP automatically 
    converts such expressions into instances of the
    <a href="class.closure.html" class="classname">Closure</a> internal class. Assigning a closure to a
    variable uses the same syntax as any other assignment, including the
    trailing semicolon:
   </p>
   <div class="example" id="example-129">
    <p><strong>Beispiel #2 Anonymous function variable assignment example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$greet </span><span style="color: #007700">= function(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Hello %s\r\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br />};<br /><br /></span><span style="color: #0000BB">$greet</span><span style="color: #007700">(</span><span style="color: #DD0000">'World'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$greet</span><span style="color: #007700">(</span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
    </div>
   </div>
   
   <p class="simpara">
    Closures may also inherit variables from the parent scope. Any such
    variables must be declared in the function header. Inheriting variables from
    the parent scope is <em class="emphasis">not</em> the same as using global
    variables. Global variables exist in the global scope, which is the same no
    matter what function is executing. The parent scope of a closure is the
    function in which the closure was declared (not necessarily the function it
    was called from). See the following example:
   </p>
   <div class="example" id="example-130">
    <p><strong>Beispiel #3 Closures and scoping</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// A basic shopping cart which contains a list of added products<br />// and the quantity of each product. Includes a method which<br />// calculates the total price of the items in the cart using a<br />// closure as a callback.<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Cart<br /></span><span style="color: #007700">{<br />    const </span><span style="color: #0000BB">PRICE_BUTTER  </span><span style="color: #007700">= </span><span style="color: #0000BB">1.00</span><span style="color: #007700">;<br />    const </span><span style="color: #0000BB">PRICE_MILK    </span><span style="color: #007700">= </span><span style="color: #0000BB">3.00</span><span style="color: #007700">;<br />    const </span><span style="color: #0000BB">PRICE_EGGS    </span><span style="color: #007700">= </span><span style="color: #0000BB">6.95</span><span style="color: #007700">;<br /><br />    protected   </span><span style="color: #0000BB">$products </span><span style="color: #007700">= array();<br />    <br />    public function </span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$product</span><span style="color: #007700">, </span><span style="color: #0000BB">$quantity</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">products</span><span style="color: #007700">[</span><span style="color: #0000BB">$product</span><span style="color: #007700">] = </span><span style="color: #0000BB">$quantity</span><span style="color: #007700">;<br />    }<br />    <br />    public function </span><span style="color: #0000BB">getQuantity</span><span style="color: #007700">(</span><span style="color: #0000BB">$product</span><span style="color: #007700">)<br />    {<br />        return isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">products</span><span style="color: #007700">[</span><span style="color: #0000BB">$product</span><span style="color: #007700">]) ? </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">products</span><span style="color: #007700">[</span><span style="color: #0000BB">$product</span><span style="color: #007700">] :<br />               </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">;<br />    }<br />    <br />    public function </span><span style="color: #0000BB">getTotal</span><span style="color: #007700">(</span><span style="color: #0000BB">$tax</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$total </span><span style="color: #007700">= </span><span style="color: #0000BB">0.00</span><span style="color: #007700">;<br />        <br />        </span><span style="color: #0000BB">$callback </span><span style="color: #007700">=<br />            function (</span><span style="color: #0000BB">$quantity</span><span style="color: #007700">, </span><span style="color: #0000BB">$product</span><span style="color: #007700">) use (</span><span style="color: #0000BB">$tax</span><span style="color: #007700">, &</span><span style="color: #0000BB">$total</span><span style="color: #007700">)<br />            {<br />                </span><span style="color: #0000BB">$pricePerItem </span><span style="color: #007700">= </span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #0000BB">__CLASS__ </span><span style="color: #007700">. </span><span style="color: #DD0000">"::PRICE_" </span><span style="color: #007700">.<br />                    </span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$product</span><span style="color: #007700">));<br />                </span><span style="color: #0000BB">$total </span><span style="color: #007700">+= (</span><span style="color: #0000BB">$pricePerItem </span><span style="color: #007700">* </span><span style="color: #0000BB">$quantity</span><span style="color: #007700">) * (</span><span style="color: #0000BB">$tax </span><span style="color: #007700">+ </span><span style="color: #0000BB">1.0</span><span style="color: #007700">);<br />            };<br />        <br />        </span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">products</span><span style="color: #007700">, </span><span style="color: #0000BB">$callback</span><span style="color: #007700">);<br />        return </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">$total</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">);;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$my_cart </span><span style="color: #007700">= new </span><span style="color: #0000BB">Cart</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Add some items to the cart<br /></span><span style="color: #0000BB">$my_cart</span><span style="color: #007700">-></span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'butter'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$my_cart</span><span style="color: #007700">-></span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'milk'</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$my_cart</span><span style="color: #007700">-></span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'eggs'</span><span style="color: #007700">, </span><span style="color: #0000BB">6</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Print the total with a 5% sales tax.<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$my_cart</span><span style="color: #007700">-></span><span style="color: #0000BB">getTotal</span><span style="color: #007700">(</span><span style="color: #0000BB">0.05</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// The result is 54.29<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
    </div>
   </div>
   
   <p class="simpara">
    Anonymous functions are currently implemented using the
    <a href="reserved.classes.html#reserved.classes.closure" class="link">
    <a href="class.closure.html" class="classname">Closure</a></a> class. This is an implementation
    detail and should not be relied upon.
   </p>
   
   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <span class="simpara">
     Anonymous functions are available since PHP 5.3.0.
    </span>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
    <span class="simpara">
     It is possible to use <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>, and <span class="function"><a href="function.func-get-args.html" class="function">func_get_args()</a></span>
     from within a closure.
    </span>
   </p></blockquote>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="functions.internal.html">Interne (eingebaute) Funktionen</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.oop5.html">Klassen und Objekte</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>