Passing parameters in PHP closure functions and using external variables

Source: Internet
Author: User
: This article mainly introduces the methods for passing parameters in the PHP closure function and using external variables. if you are interested in the PHP Tutorial, refer to it. This article describes how to pass parameters in PHP closure functions and use external variables. We will share this with you for your reference. The details are as follows:

Write two methods in the Laravel controller. one is to create a closure function internally, the other is to execute the passed closure function, test the writing method of the closure, and use external variables, and parameters of the closure function. As follows:

// Test the closure for passing parameters and use the external variable public function testClosure ($ t1, $ t2) {$ closure = function ($ param1, $ param2) use ($ t1, $ t2) {echo $ param1. $ param2. $ t1. $ t2 ;}; $ this-> execClosure ('test. closure ', $ closure);} // execute the Closure function protected function execClosure ($ name, closure $ Closure) {echo 'closure func name :'. $ name; echo'
'; $ Closure ('p1', 'P2 ');}

Add a route in routes. php:

The code is as follows:

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

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

Browser output result:

Closure func name: test. closurep1p2hehe1he2

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.