你什么时候需要使用它们

你什么时候需要使用它们

本文介绍了PHP中的闭包...什么,准确地说,它们是什么,你什么时候需要使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 所以我编程在一个漂亮,最新,面向对象的时尚。我经常利用PHP实现的OOP的各个方面,但我想知道什么时候我需要使用闭包。So I'm programming along in a nice, up to date, object oriented fashion. I regularly make use of the various aspects of OOP that PHP implements but I am wondering when might I need to use closures. Any experts out there that can shed some light on when it would be useful to implement closures?推荐答案 PHP将支持本机的闭包5.3。一个闭包是好的,当你想要一个局部函数,只用于一些小的,具体的目的。 关于封闭的RFC 提供了一个很好的示例:PHP will support closures natively in 5.3. A closure is good when you want a local function that's only used for some small, specific purpose. The RFC for closures give a good example:function replace_spaces ($text) { $replacement = function ($matches) { return str_replace ($matches[1], ' ', ' ').' '; }; return preg_replace_callback ('/( +) /', $replacement, $text);}这样可以定义替换 在 replace_spaces()内部局部使用的函数: 2)让人们三年下来想知道为什么全局定义的函数只能在另一个函数中使用This lets you define the replacement function locally inside replace_spaces(), so that it's not:1) Cluttering up the global namespace2) Making people three years down the line wonder why there's a function defined globally that's only used inside one other function它使事情有组织。注意函数本身没有名字,它只是被定义和赋值为 $ replacement 的引用。It keeps things organized. Notice how the function itself has no name, it simply is defined and assigned as a reference to $replacement.但请记住,你必须等待PHP 5.3:)But remember, you have to wait for PHP 5.3 :) 这篇关于PHP中的闭包...什么,准确地说,它们是什么,你什么时候需要使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-01 12:28