PHP Closed-Package function and the method of using external variables _php tips

Source: Internet
Author: User
Tags closure php database

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.

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.