PHP function Summary (closure function, anonymous function)

Source: Internet
Author: User

Summary of PHP functions

1. General functions

2. Variable functions

function myfun ($a) {    echo$a;} $b = "Myfun"; $b ("Test");

3. Anonymous function (can implement closures)

An anonymous function (Anonymous functions), also called a closure function (Closures), allows temporary creation of a function without a specified name, often as a parameter to a callback function (callback), and of course other applications

$func function () {}; // to bring a semicolon $func // called Var_dump ($func); // returns the object Type objects (Closure) #1 (0) {}

4. Closure function: The anonymous function in the normal function as a parameter in and out, you can also be returned, the implementation of a simple closure.

In layman's terms, a child function can use a local variable in the parent function, which is called a closure.

Features of closures:

1. As a reference to a function variable, when the function returns, it is in the active state.

2. A closure is when a function returns, a stack that does not release resources

In fact, the above two points can be synthesized a little, that is, when the closure function returns, the function internal variables are active, the function of the stack area is still retained.

functionMyFunc () {$a=10; $b=11; $one=function($str) Use(&$a,$b){//Use reference outer variable does not add & copy does not affect parent function value        Echo $a=$a+2; Echo' <br/> '; Echo $b=$b+2; Echo' <br/> '; Echo $str;    }; Echo $a; Echo'---<br/> '; Echo $b; Echo'---<br/> '; return $one;}$a=MyFunc ();$a(' Hello ');

The parent function returns the anonymous function as the return value, one of the closures:

5. Intrinsic functions

Extended knowledge php:use Use of keywords

1. Namespaces

2. Closure function context

3.Trait code reuse Reference .... (Refer to http://php.net/manual/zh/language.oop5.traits.php)

Previous post: http://www.cnblogs.com/fps2tao/p/8727248.html

PHP function Summary (closure function, anonymous function)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.