Javascript closures vs PHP closures, what's the difference?

后端 未结 3 778
遥遥无期
遥遥无期 2021-01-29 20:57

What are the differences between closures in JS and closures in PHP? Do they pretty much work the same way? Are there any caveats to be aware of when writing closures in PHP? <

3条回答
  •  鱼传尺愫
    2021-01-29 21:32

    The only thing I've found in PHP (that is totally cool and really handy!) is the ability to use them as getters and setters in classes which was always a nightmare to achieve before, JavaScript can be used in the same way but they do both act almost identically from what I've seen.

    I'm not sure about the namespacing convention differences between the two but as @Rijk pointed out there is a section on the PHP website dedicated to them

    They are also really great for...

    Looping through items with a controller rather than a new for/each loop on the page

    Great for supplying as arguments to functions/classes

    Whats annoying about them is...

    You can't typecast them, since they're just functions...

提交回复
热议问题