


PHP 53's new closure syntax introduces function use {} jquery function objective function likelihood function
Reprinted original post address: 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>
The above introduces the new closure syntax function use {} in PHP 53, including function content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

Usage and Function of Vue.use Function Vue is a popular front-end framework that provides many useful features and functions. One of them is the Vue.use function, which allows us to use plugins in Vue applications. This article will introduce the usage and function of the Vue.use function and provide some code examples. The basic usage of the Vue.use function is very simple, just call it before Vue is instantiated, passing in the plugin you want to use as a parameter. Here is a simple example: //Introduce and use the plug-in

The clearstatcache() function is used to clear the file status cache. PHP caches the information returned by the following functions −stat()lstat()file_exists()is_writable()is_readable()is_executable()is_file()is_dir()filegroup()fileowner()filesize()filetype()fileperms() What to do To provide better performance. Syntax voidclearstatecache() Parameter NA Return value clearstatcache(

The file_exists method checks whether a file or directory exists. It accepts as argument the path of the file or directory to be checked. Here's what it's used for - it's useful when you need to know if a file exists before processing it. This way, when creating a new file, you can use this function to know if the file already exists. Syntax file_exists($file_path) Parameters file_path - Set the path of the file or directory to be checked for existence. Required. Return file_exists() method returns. Returns TrueFalse if the file or directory exists, if the file or directory does not exist Example let us see a check for "candidate.txt" file and even if the file

The usage of js function function is: 1. Declare function; 2. Call function; 3. Function parameters; 4. Function return value; 5. Anonymous function; 6. Function as parameter; 7. Function scope; 8. Recursive function.

With the development of the Internet, SOA (service-oriented architecture) has become an important technical architecture in today's enterprise-level systems. Services in the SOA architecture can be reused, reorganized and extended, while also simplifying the system development and maintenance process. As a widely used Web programming language, PHP also provides some function libraries for implementing SOA. Next, we will detail how to use SOA functions in PHP. 1. The basic concept of SOA. SOA is a distributed system development idea and architecture.
