PHP anonymous functions and considerations, detailed PHP anonymous _php tutorial

Source: Internet
Author: User

PHP anonymous functions and considerations, detailed PHP Anonymous


php5.3 not only introduced anonymous functions and many more and more new features, let's look at the PHP anonymous functions and considerations, the details are as follows

PHP5.2 before : AutoLoad, PDO, and mysqli, type constraints
PHP5.2: JSON Support
PHP5.3: deprecated features, anonymous functions, New magic methods, namespaces, late static bindings, Heredoc and Nowdoc, const, ternary operators, Phar
PHP5.4: Short Open Tag, array shorthand form, Traits, built-in Web server, detail modified
PHP5.5:yield, List () for foreach, detail modification
PHP5.6: constant Enhancement, variable function parameters, namespace enhancement

Now basically use PHP5.3 later version, but feel common a phenomenon is a lot of new features, after so long time, not fully popularized, rarely used in the project.

Look at the PHP anonymous function:

' Test ' = function () {  return ' test '},

The definition of an anonymous PHP function is simply to assign a value to a variable, except that the value is a function.

The above is configured with the YII framework to configure the components file, plus a test configuration.

What is the PHP anonymous function?

Look at the official explanation:

The anonymous function (Anonymous functions), also called the closure function (closures), allows temporary creation of a function without a specified name. The value most often used as the callback function (callback) parameter. Of course, there are other applications as well.

anonymous function Example

<?phpecho preg_replace_callback (' ~-([A-z]) ~ ', function ($match) {return Strtoupper ($match [1]);}, ' Hello-world '); /Output helloworld?>

The closure function can also be used as the value of a variable. PHP automatically converts this expression into an object instance of the built-in class Closure. Assigning a closure object to a variable is the same as the syntax for assigning a value to a normal variable, with a semicolon at the end:

Example of an anonymous function variable assignment

<?php$greet = function ($name) {printf ("Hello%s\r\n", $name);}; $greet (' World '); $greet (' PHP '); >

Closures can inherit variables from the parent scope. Any such variables should be passed in using the use language structure.

Inherit variables from parent scope

Considerations for anonymous functions in PHP

After php5.3, PHP joins the use of anonymous functions, today in the use of anonymous errors, can not be declared and used as PHP functions, detailed look at the code

$callback =function () {  

Print out is AA;

Look at the following example:

Echo $callback (); $callback =function () {  

This is an error! $callback is not declared, but using PHP's own declared function will not be an error!

function callback () {  return ' AA ';} Echo callback ();//aa  Echo Callback ();//aa function callback () {  

Both of these are printed out AA;

When using anonymous functions, the anonymous function as a variable, must be declared in advance, JS is the same!!!!!

The above is for everyone to introduce the PHP anonymous functions and considerations, I hope that everyone's learning has helped.

Articles you may be interested in:

    • PHP Array function array_multisort () Usage Example analysis
    • PHP computes the sum and product of values in an array (Array_sum and array_product functions)
    • PHP Math function Summary (classic worth collecting)
    • Summary of common array function usage in PHP
    • PHP anonymous function and use clause usage instance
    • Simply talk about the strlen function in PHP
    • Analysis of source code of Array_keys and Array_unique functions in PHP
    • Php method for querying and deleting multiple columns of data in a database (using array functions)
    • PHP Powerful Time-conversion function strtotime
    • PHP function Timeout processing method
    • Probe into the anonymous function of PHP's closure (Closure)
    • php function Import_request_variables () usage analysis

http://www.bkjia.com/PHPjc/1117076.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117076.html techarticle detailed PHP anonymous functions and considerations, detailed PHP anonymous php5.3 not only introduced anonymous functions and many more new features, let's look at the PHP anonymous function and attention ...

  • 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.