当前位置:Gxlcms > PHP教程 > php闭包实验

php闭包实验

时间:2021-07-01 10:21:17 帮助过:30人阅读

class IArray extends ArrayObject  
{      
static function make($array)      
{          
return new self($array);      
}
function map($func)      
{          
$res = new self();
foreach ($this as $k => $v)   
{   
$res[$k] = $func($k, $v);          
}
return $res;      
}
function filter($func)      
{          
$res = new self();
foreach ($this as $k => $v)   
{              
if ($func($k, $v))   
{                  
$res[$k] = $v;             
}      
}
return $res;      
}
}
$nums = array(10,20,30,40);
$res = IArray::make($nums)->filter(function($k, $v) { return $v > 15; })->map(function($k, $v) { return $v * 2; });
print_r($res);

人气教程排行