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