Table of Contents
PHP闭包函数传参及使用外部变量的方法,php变量
您可能感兴趣的文章:
Home php教程 php手册 PHP闭包函数传参及使用外部变量的方法,php变量

PHP闭包函数传参及使用外部变量的方法,php变量

Jun 13, 2016 am 08:44 AM
php global variables external variables closure function

PHP闭包函数传参及使用外部变量的方法,php变量

本文实例讲述了PHP闭包函数传参及使用外部变量的方法。分享给大家供大家参考,具体如下:

在Laravel控制器写两个方法,一个是在内部创建一个闭包函数,一个是执行传过来的闭包函数,测试闭包的写法,use使用外部变量,及闭包函数的传参。如下:

//测试闭包传参及use使用外部变量
public function testClosure($t1, $t2)
{
  $closure = function ($param1, $param2) use ($t1, $t2) {
    echo $param1.$param2.$t1.$t2;
  };
  $this->execClosure('test.closure', $closure);
}
//执行闭包函数
protected function execClosure($name, Closure $closure)
{
  echo 'Closure func name:'.$name;
  echo '<br>';
  $closure('p1', 'p2');
}

Copy after login

在routes.php添加路由:
复制代码 代码如下:Route::get('/test/closure/{t1}/{t2}',['uses'=>'TestController@testClosure']);

访问www.example.com/test/closure/hehe1/hehe2

浏览器输出结果:

Closure func name:test.closure
p1p2hehe1hehe2

Copy after login

转自:小谈博客 http://www.tantengvip.com/2016/03/php-closure-use/

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • PHP闭包函数详解
  • php的闭包(Closure)匿名函数初探
  • php的闭包(Closure)匿名函数详解
  • PHP中的闭包(匿名函数)浅析
  • PHP闭包实例解析
  • PHP闭包(Closure)使用详解
  • PHP用反撇号执行外部命令
  • PHP 执行系统外部命令 system() exec() passthru()
  • 在PHP中执行系统外部命令
  • PHP 中执行系统外部命令
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles