A summary of PHP closure functions and closure objects

Source: Internet
Author: User
Tags closure
doubt

If you think it's a bit confusing to understand the closure function, put it aside and ask yourself a question:
Object-oriented programming language for the reuse of code is mainly implemented by inheritance, that function-oriented code reuse capabilities through what to achieve it.

The first thing you might think of is a function nesting, which is to organize your code with independent child functions. What about the functions of Array_walk (), Array_udiff () that resemble PHP? Do you think that with the anonymous function, Array_walk (), Array_udiff () Two functions can be so well reused.

So according to the most basic understanding, I personally think that the main purpose of the closure function is to design the function reuse, if a language does not support closure functions, then the ability to reuse the function will be less, but through the class-oriented interface to programming, can also be a good reuse of code, so all the roots are for code reuse.

The closure function is so difficult to understand, I think it's too many posts on the web. The ability to do a lot of different things based on closure functions, such as how to write a for loop, how to add a function dynamically to a class, is really one of the capabilities of the closure function, but it's not the source of the closure function design, so it will Let readers not understand the closure function. A better way to understand this is to reverse it, to understand why the closure function is needed, how the closure function works, and then to understand what scenarios the closure functions have.

General to understand, I think, PHP inside the closure function is designed to reuse functions of language features, like JavaScript, such as with a function-oriented programming language, can be in the closure function to access the specified domain variables, in PHP using the use of keyword to achieve. Closure

PHP has a function-oriented programming characteristics, but also object-oriented programming language, PHP will automatically transform the closure function into built-in class Closure object instances, relying on Closure object instances to add more capabilities to the closure function.
For example, the two functions of closure:
closure::bind-copies a closure that binds the specified this object and the class scope. closure::bindto-copies the current closure object, binding the specified this object and the class scope. closure::bindto-copies the current closure object, binding the specified this object and the class scope.

The literal description of the two functions is confusing, and it is straightforward to understand that, by using these two functions, you can specify the direction of the $this variable in the closure function and the scope of the closure function.

The concrete bottom how to control this scope is not yet time to see, but know the scope of the concept is good.

Well, with the basic intent to see the example, there are too many examples, they do not write:
http://blog.csdn.net/wuxing26jiayou/article/details/51067190
Http://www.cnblogs.com/yjf512/archive/2012/10/29/2744702.html

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.