update : 2015.11.03
php.shukuma.com검색:
|
익명 함수Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses. Example #1 Anonymous function example
<?php Closures can also be used as the values of variables; PHP automatically converts such expressions into instances of the Closure internal class. Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon: Example #2 Anonymous function variable assignment example
<?php Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct. Example #3 Inheriting variables from the parent scope
<?php 위 예제의 출력 예시: Notice: Undefined variable: message in /example.php on line 6 NULL string(5) "hello" string(5) "hello" string(5) "hello" string(5) "world" string(11) "hello world" Inheriting variables from the parent scope is not 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: Example #4 Closures and scoping
<?php Anonymous functions are implemented using the Closure class. 변경점
주의
|