This article describes the PHP closure function parameters and the use of external variables method. Share to everyone for your reference, specific as follows:
Write two methods in the Laravel controller, one is to create a closure function internally, one is to perform the closure function, test the closure of the writing, use external variables, and closure function of the reference. 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);
}
Execute closure functions
protected function Execclosure ($name, Closure $closure)
{
echo ' Closure func name: '. $name;
Echo ' <br> ';
$closure (' P1 ', ' P2 ');
}
To add a route to routes.php:
Copy Code code as follows:
Route::get ('/test/closure/{t1}/{t2} ', [' Uses ' => ' testcontroller@testclosure ']);
Visit www.example.com/test/closure/hehe1/hehe2
Browser Output results:
Closure func name:test.closure
p1p2hehe1hehe2
Turn from: Small Talk blog http://www.tantengvip.com/2016/03/php-closure-use/
For more information on PHP-related content readers can view the site topics: "PHP operation Office Document tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course", "PHP string (string) Summary of usage, Getting Started tutorial on Php+mysql database operations, and summary of common PHP database operations techniques
I hope this article will help you with the PHP program design.