Home > Backend Development > PHP Tutorial > Detailed explanation of PHP anonymous functions and anonymous classes

Detailed explanation of PHP anonymous functions and anonymous classes

小云云
Release: 2023-03-21 10:06:01
Original
1916 people have browsed it

Anonymous functions, also called closures, allow you to temporarily create a function without a specified name. The value most commonly used as a callback function argument.

Closures can inherit variables from the parent scope. Any such variables should be passed in using the use language construct.

Used in built-in functions and custom functions respectively, sample code:

//递归为数组的每个元素应用回调函数
$arr = [    'name' => ' joker',    
'content' => [        'date' => ' 
2018-03-07 11:11:11',        
'doing' => ' 测试匿名函数 '    
]];function array_map_recursive(callable $func, array $array)
 {    return filter_var($array, FILTER_CALLBACK, ['options' => 
 $func]);}//调用示例$prefix = '前缀:';$arr1 = array_map_recursive(function($v) use($prefix)
  {    return $prefix.trim($v);}, $arr);$arr2 = array_map(function($v) 
  {    return is_string($v)?trim($v):$v;}, $arr);
Copy after login

Related recommendations:

A summary of several PHP anonymous function usage examples

PHP anonymous functions and closures

Usage of php anonymous functions

The above is the detailed content of Detailed explanation of PHP anonymous functions and anonymous classes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template