PHP closure function parameters and methods of using external variables

Source: Internet
Author: User
Tags getting started with php php programming
In this paper, we describe the parameters of PHP closure function and the methods of using external variables. Share to everyone for your reference, as follows:

In the Laravel controller write two methods, one is to create a closure function in the internal, one is to execute the passed closure function, test the closure of the wording, use the external variables, and the closure function of the parameters. As follows:

Test closed Bao and use external variables public function testclosure ($t 1, $t 2) {  $closure = function ($param 1, $param 2) using ($t 1, $t 2) {    echo $param 1. $param 2. $t 1. $t 2;  };  $this->execclosure (' test.closure ', $closure);} Executes the closure function protected functions execclosure ($name, Closure $closure) {  echo ' Closure func name: '. $name;  Echo '
'; $closure (' P1 ', ' P2 ');}

To add a route in routes.php:

Copy the Code code as follows:

Route::get ('/test/closure/{t1}/{t2} ', [' uses ' = ' testcontroller@testclosure ']);

Visit www.example.com/test/closure/hehe1/hehe2

The results of the browser output:

Closure func name:test.closurep1p2hehe1hehe2

Turn from: Small Talk blog http://www.tantengvip.com/2016/03/php-closure-use/

More readers interested in PHP related content can view this site topic: "PHP Operations Office Document Tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP Object-oriented Programming tutorial", "PHP string (string) Usage summary, "Getting Started with Php+mysql database operation" and "PHP common database Operation Skills Summary"

I hope this article is helpful to you in PHP programming.

The above introduces the PHP closure function and the use of external variables, including the contents of the content, I hope that the PHP tutorial interested in a friend helpful.

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