【PHP闭包定义与使用简单示例php技巧】教程文章相关的互联网学习教程文章

PHP 中的闭包函数和匿名函数【代码】

闭包函数 闭包函数通常作为函数中的函数使用。 <?php $foo = function($s) {echo $s; }; $foo('hello');<?php function test() {$a = 1;$b = 2;$foo = function($s) use($a, $b) {echo $s . ($a + $b);};$foo('hello'); } test();<?php // 返回一个闭包函数供外部调用 function test() {$foo = function($s) {echo $s;}; return $foo; } $res = test(); $res('hello')匿名函数 匿名函数通常作为回调函数的参数使用。 function fo...

如何将PHP闭包(lambda函数)保存到$this-> var中,并稍后从同一类的另一个方法调用它?【代码】

大家好,我想知道如何执行以下操作: 我有一堂课class SomeClass {private $someVar;public function Init($func) {$this->someVar = $func;}public function DoSomething() {$this->someVar("asdasdasd");} }$obj = new SomeClass(); $obj->Init(function ($param){var_dump($param);}); $obj->DoSomething();当我调用DoSomething方法时,我得到一个错误,即SomeClass :: someVar()是未定义的方法.但是当我使用调试器时,我看到它是一...

如何在PHP的闭包中插入变量?【代码】

function updateParameters($landing) {$addValue = function ($final,$parametername,$value){$value1=str_replace ( '{landing}' , $landing ,$value );$_GET[$parametername]=$value1;return "";};doSomethingWith20Params($addValue, "") ; }基本上,$addValue是一个函数.参数号不能更改.那是因为doSomethingWith20Params($addValue,“”);需要具有3个参数的函数. 在PHP中我得到了5 446.6299 486504 doSomethingWith20...

在PHP 5.3的闭包中是否有针对$this的解决方法?【代码】

我的IDE警告我,PHP 5.4之前的闭包中不允许$this.有没有从5.3.10升级PHP的解决方法?请参阅下面的fire()方法:<?phpuse Illuminate\Console\Command; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument;class UpdateItemImageSizes extends Command {/*** The console command name.** @var string*/protected $name = 'namespace:updateimagesizes';/*** The console command ...

在PHP的类属性中存储闭包函数【代码】

好的,我有下面的代码<?phpclass foo{public $bar = NULL;public function boo(){$this->bar();}}$mee = new foo();//save a closure function on the property$mee->bar = function(){echo 'hahaha';};//invoke the closure function by using a class method$mee->boo(); ?>你可以看到它在这里运行http://codepad.org/s1jhi7cv 现在我想要在这里将闭包函数存储在class方法上. 我在这里阅读有关文档的信息时,可能会关闭孔http://ph...

PHP 4中的匿名函数(lambda,闭包)【代码】

PHP 4中有技巧实现返回函数的函数吗?我希望以下代码可以工作:function xxx($a) {return function($b) {print "a=$a, b=$b \n";} }$f1 = xxx(1); $f1(2);不幸的是,在PHP 4中运气不佳.可能它在PHP 5中有效,但我限于PHP 4. 我尝试使用OO解决,但再次失败(类声明可能不嵌套):class Closure {function run($a) {print "raise: NotImplementedException, instead: $a\n";} }class WantCheckNesting extends Closure {function run($a, ...

PHP闭包和隐式全局变量范围【代码】

有没有办法可以隐含地将顶级变量声明为闭包中使用的全局变量? 例如,如果使用如下代码:$a = 0; //A TOP-LEVEL VARIABLEAlpha::create('myAlpha')->bind(DataSingleton::getInstance()->query('c'))->addBeta('myBeta', function($obj){$obj->bind(DataSingleton::getInstance()->query('d'))->addGamma('myGamma', function($obj){$obj->bind(DataSingleton::getInstance()->query('a'))->addDelta('myDelta', function($obj){$ob...

在PHP中,什么是闭包,为什么它使用“use”标识符?【代码】

我正在检查一些PHP 5.3.0的功能,并在网站上遇到了一些非常有趣的代码:public function getTotal($tax) {$total = 0.00;$callback =/* This line here: */function ($quantity, $product) use ($tax, &$total){$pricePerItem = constant(__CLASS__ . "::PRICE_" .strtoupper($product));$total += ($pricePerItem * $quantity) * ($tax + 1.0);};array_walk($this->products, $callback);return round($total, 2); }作为anonymous ...

php调用回调与闭包

根据参数形式不同有两个函数 1、参数个数未知 call_user_func ( callable $callback [, mixed $parameter [, mixed $… ]] ) : mixed 示例:call_user_func(func, p1, p2, …); 2、参数为一个数组 call_user_func_array ( callable $callback , array $param_arr ) : mixed 示例:call_user_func_array(func, arr1);

php – 带有匿名函数和闭包的cURL WRITEFUNCTION回调【代码】

我正在使用cURL的CURLOPT_WRITEFUNCTION选项指定一个回调来处理来自cURL请求的数据.$serverid=5; $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.whatever.com'); curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $string) { return readCallback($ch, $string, $serverid);} ); curl_exec($ch);function readCallback($ch, $string, $serverid) {echo "Server #", $serverid, " | ", $string;return str...

使PHP闭包函数对PHP 5.2安全【代码】

以下函数适用于PHP> 5.3但旧版本中的错误.如何修改它以使其安全?function _iniloader_get_dirs($dir) {$dirs = array_filter(scandir($dir), function ($item) use ($dir) {return (is_dir($dir.'/'.$item) && $item != "." && $item != "..");});// Use array_values to reset the array keys:return array_values($dirs); }5.2错误:Parse error: syntax error, unexpected T_FUNCTION … online 2解决方法:你可以轻松地完成闭合...

PHP中的实际闭包<5.3【代码】

有没有办法用PHP为早于5.3的语言版本编写真正的闭包(5.3为匿名函数添加了use关键字)? 我PHP 5.3我可以写:function make_adder($x) {return function($to) use ($x) {return $to + $x;}; } $add5 = make_adder(5); $add5(100); # => 105如何在函数内部使用这种定义函数的模式,内部函数可以访问外部函数变量?解决方法:以下内容适用于这种简单的情况:function make_adder($x) {return create_function('$to', 'return '.var_expor...

从PHP闭包中读取“this”和“use”参数【代码】

当您创建一个在PHP中返回闭包的方法时:class ExampleClass {public function test() {$example = 10;return function() use ($example) {return $example;};} }print_r的结果包含this(其方法创建闭包的类)和static,它看起来是闭包的use()语句中绑定的值:$instance = new ExampleClass(); $closure = $instance->test();print_r($closure);生产:Closure Object ([static] => Array ([example] => 10)[this] => ExampleClass Obje...

如何在使用闭包时修复php模式缩进【代码】

我在Emacs中使用php-mode,当我使用闭包作为参数时,它工作得很好:$app->get('/', function() use ($app) {echo "foo";});它接缝当函数在函数调用内部时,缩进加倍.如何解决这个问题? 编辑 如何使它看起来像这样(与javascript模式处理匿名函数相同).$app->get('/', function() use ($app) {echo "foo"; });解决方法:如果你把点放在第一行的末尾并按下C-c C-o,你可以看到cc-mode认为你在中间的语法结构,并自定义它如何缩进该构造.我当...

闭包如何帮助创建DSL /流畅的界面:PHP示例?【代码】

你能举个例子,用PHP来说明闭包是如何帮助创建DSL(流畅的界面)的吗? 编辑:以下问题中接受的答案讲述了嵌套闭包.如果有人可以将这个例子翻译成PHP也会有所帮助:Experience with fluent interfaces? I need your opinion!解决方法:这是我能想到的第一个例子,它不是很好,但它给你一个想法:$db = new Database(); $filteredList = $db->select()->from('my_table')->where('id', 9)->run()->filter(function($record){// apply some...