How PHP anonymous and use clauses work

Source: Internet
Author: User
Look at the code below.

function test () {$param 2 = ' every ';//Returns an anonymous function ($param 1) use ($param 2) {///using clause to let anonymous function uses its scope variable $param2. = ' O Ne ';p rint $param 1. ' ' . $param 2;};} $anonymous _func = Test (); $anonymous _func (' hello ');

The output is Hello World

$param 1 and $param2 are closure variables

The following way outputs Hello everyone

function test () {$param 2 = ' everyone '; $func = function ($param 1) use ($param 2) {///using clause let anonymous function uses the variable of its parent scope print $param 1. ' ' . $param 2;}; $param 2 = ' everybody '; return $func;} $anonymous _func = Test (); $anonymous _func (' hello ');

The following way outputs Hello everybody

More than one reference in $param 2

function test () {$param 2 = ' everyone '; $func = function ($param 1) using (& $param 2) {//USE clause let anonymous function uses the variable of its parent scope print $param 1 . ' ' . $param 2;}; $param 2 = ' everybody '; return $func;} $anonymous _func = Test (); $anonymous _func (' hello ');
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.