PHP 5.3 closure syntax is an anonymous function, which can bring many different experiences to developers. This article describes how to use it. It is a little different from the closure of JavaScript.For example, you can use the PHP 5.3 closure syntax now.
- $Closure=Function($ Param) {echo $ param ;};
- // This one takes value of someVar and "stores" it in the closure's scope even if
- // We later change the value of someVar outside it. We assume that $ somerVar is defined before this
- $Closure2=Function($ Param) use ($ someVar) {echo $ param. ''. $ someVar ;};
For example, the closure Syntax of PHP 5.3 is useful in output HTML:
- FunctionItem_list (Array $ Items,$ Formatter= Null ){
- // Create the default formatter
- If($ Formatter= Null ){
- $ Formatter=Function($ Row){
- Return '<P>'.$ Row.'</P>';
- };
- }
- $ Html='<H2> Listing: ;
- Foreach($ Items As $ Item){
- $ Html. =$ Formatter($ Item);
- }
- Return $ Html;
- }
The above is the use of two different PHP 5.3 closure syntaxes introduced in this article. I hope it will be helpful to you.