재인쇄된 원본 게시물 주소: http://blog.csdn.net/lgg201/article/details/6127564
<code><span><span><span><?php</span><span><span>function</span><span>callback</span><span>(<span>$callback</span>)</span> {</span><span>$callback</span>(); } <span>//输出: This is a anonymous function.<br />/n</span><span>//这里是直接定义一个匿名函数进行传递, 在以往的版本中, 这是不可用的.</span><span>//现在, 这种语法非常舒服, 和<a href='http://lib.csdn.net/base/18' title='JavaScript知识库' target='_blank'>javascript</a>语法基本一致, 之所以说基本呢, 需要继续向下看</span><span>//结论: 一个舒服的语法必然会受欢迎的.</span> callback(<span><span>function</span><span>()</span> {</span><span>print</span><span>"This is a anonymous function.<br />/n"</span>; }); <span>//输出: This is a closure use string value, msg is: Hello, everyone.<br />/n</span><span>//这里首先定义了一个闭包, 这次户口本上有名字了...</span><span>//use, 一个新鲜的家伙...</span><span>//众所周知, 闭包: 内部函数使用了外部函数中定义的变量.</span><span>//在PHP新开放的闭包语法中, 我们就是用use来使用闭包外部定义的变量的.</span><span>//这里我们使用了外部变量$msg, 定义完之后, 又对其值进行了改变, 闭包被执行后输出的是原始值</span><span>//结论: 以传值方式传递的基础类型参数, 闭包use的值在闭包创建是就确定了.</span><span>$msg</span> = <span>"Hello, everyone"</span>; <span>$callback</span> = <span><span>function</span><span>()</span><span>use</span><span>(<span>$msg</span>)</span> {</span><span>print</span><span>"This is a closure use string value, msg is: $msg. <br />/n"</span>; }; <span>$msg</span> = <span>"Hello, everybody"</span>; callback(<span>$callback</span>); <span>//输出: This is a closure use string value lazy bind, msg is: Hello, everybody.<br />/n</span><span>//换一种引用方式, 我们使用引用的方式来use</span><span>//可以发现这次输出是闭包定义后的值...</span><span>//这个其实不难理解, 我们以引用方式use, 那闭包use的是$msg这个变量的地址</span><span>//当后面对$msg这个地址上的值进行了改变之后, 闭包内再输出这个地址的值时, 自然改变了.</span><span>$msg</span> = <span>"Hello, everyone"</span>; <span>$callback</span> = <span><span>function</span><span>()</span><span>use</span><span>(&<span>$msg</span>)</span> {</span><span>print</span><span>"This is a closure use string value lazy bind, msg is: $msg. <br />/n"</span>; }; <span>$msg</span> = <span>"Hello, everybody"</span>; callback(<span>$callback</span>); <span>//输出: This is a closure use object, msg is: Hello, everyone.<br />/n</span><span>//闭包中输出的是之前被拷贝的值为Hello, everyone的对象, 后面是对$obj这个名字的一个重新赋值.</span><span>//可以这样考虑</span><span>//1. obj是对象Hello, everyone的名字</span><span>//2. 对象Hello, everyone被闭包use, 闭包产生了一个对Hello, everyone对象的引用</span><span>//3. obj被修改为Hello, everybody这个对象的名字</span><span>//4. 注意, 是名字obj代表的实体变了, 而不是Hello, everyone对象, 那自然闭包的输出还是前面的Hello, everyone</span><span>$obj</span> = (object) <span>"Hello, everyone"</span>; <span>$callback</span> = <span><span>function</span><span>()</span><span>use</span><span>(<span>$obj</span>)</span> {</span><span>print</span><span>"This is a closure use object, msg is: {$obj->scalar}. <br />/n"</span>; }; <span>$obj</span> = (object) <span>"Hello, everybody"</span>; callback(<span>$callback</span>); <span>//输出: This is a closure use object, msg is: Hello, everybody.<br />/n</span><span>//还是按照上面的步骤, 按部就班的来吧:</span><span>//1. obj名字指向Hello, everyone对象</span><span>//2. 闭包产生一个引用指向Hello, everyone对象</span><span>//3. 修改obj名字指向的对象(即Hello, everyone对象)的scalar值</span><span>//4. 执行闭包, 输出的自然是Hello, everybody, 因为其实只有一个真正的对象</span><span>$obj</span> = (object) <span>"Hello, everyone"</span>; <span>$callback</span> = <span><span>function</span><span>()</span><span>use</span><span>(<span>$obj</span>)</span> {</span><span>print</span><span>"This is a closure use object, msg is: {$obj->scalar}. <br />/n"</span>; }; <span>$obj</span>->scalar = <span>"Hello, everybody"</span>; callback(<span>$callback</span>); <span>//输出: This is a closure use object lazy bind, msg is: Hello, everybody.<br />/n</span><span>//闭包引用的是什么呢? &$obj, 闭包产生的引用指向$obj这个名字所指向的地址.</span><span>//因此, 无论obj怎么变化, 都是逃不脱的....</span><span>//所以, 输出的就是改变后的值</span><span>$obj</span> = (object) <span>"Hello, everyone"</span>; <span>$callback</span> = <span><span>function</span><span>()</span><span>use</span><span>(&<span>$obj</span>)</span> {</span><span>print</span><span>"This is a closure use object lazy bind, msg is: {$obj->scalar}. <br />/n"</span>; }; <span>$obj</span> = (object) <span>"Hello, everybody"</span>; callback(<span>$callback</span>); <span>/** * 一个利用闭包的计数器产生器 * 这里其实借鉴的是<a href='http://lib.csdn.net/base/11' title='Python知识库' target='_blank'>python</a>中介绍闭包时的例子... * 我们可以这样考虑: * 1. counter函数每次调用, 创建一个局部变量$counter, 初始化为1. * 2. 然后创建一个闭包, 闭包产生了对局部变量$counter的引用. * 3. 函数counter返回创建的闭包, 并销毁局部变量, 但此时有闭包对$counter的引用, * 它并不会被回收, 因此, 我们可以这样理解, 被函数counter返回的闭包, 携带了一个游离态的 * 变量. * 4. 由于每次调用counter都会创建独立的$counter和闭包, 因此返回的闭包相互之间是独立的. * 5. 执行被返回的闭包, 对其携带的游离态变量自增并返回, 得到的就是一个计数器. * 结论: 此函数可以用来生成相互独立的计数器. */</span><span><span>function</span><span>counter</span><span>()</span> {</span><span>$counter</span> = <span>1</span>; <span>return</span><span><span>function</span><span>()</span><span>use</span><span>(&<span>$counter</span>)</span> {</span><span>return</span><span>$counter</span> ++;}; } <span>$counter1</span> = counter(); <span>$counter2</span> = counter(); <span>echo</span><span>"counter1: "</span> . <span>$counter1</span>() . <span>"<br />/n"</span>; <span>echo</span><span>"counter1: "</span> . <span>$counter1</span>() . <span>"<br />/n"</span>; <span>echo</span><span>"counter1: "</span> . <span>$counter1</span>() . <span>"<br />/n"</span>; <span>echo</span><span>"counter1: "</span> . <span>$counter1</span>() . <span>"<br />/n"</span>; <span>echo</span><span>"counter2: "</span> . <span>$counter2</span>() . <span>"<br />/n"</span>; <span>echo</span><span>"counter2: "</span> . <span>$counter2</span>() . <span>"<br />/n"</span>; <span>echo</span><span>"counter2: "</span> . <span>$counter2</span>() . <span>"<br />/n"</span>; <span>echo</span><span>"counter2: "</span> . <span>$counter2</span>() . <span>"<br />/n"</span>; <span>?></span></span></span></code>
위 내용은 함수 내용을 포함하여 PHP 53에서 {}를 사용하는 새로운 클로저 구문 함수를 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.