PHP closure function passing parameters and using external variables, php Variables

Source: Internet
Author: User

PHP closure function passing parameters and using external variables, php Variables

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 '<br>'; $ closure ('p1', 'p2 ');}

Add a route in routes. php:
Copy codeThe 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.