首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

create_function

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

create_function - 创建一个匿名(lambda-style)函数

警告

此功能已被弃用的PHP 7.2.0的。依靠这个功能是非常不鼓励的。

描述

代码语言:javascript
复制
string create_function ( string $args , string $code )

从传递的参数创建一个匿名函数,并为其返回一个唯一名称。

警告

此函数内部执行eval(),因此具有与eval()相同的安全问题。另外它具有不良的性能和内存使用特性。

如果您使用的是PHP 5.3.0或更新版本,则应该使用本机匿名函数。

参数

通常这些参数将作为单引号分隔的字符串传递。使用单引号字符串的原因是保护变量名不被解析,否则,如果使用双引号,则需要转义变量名,例如\ $ avar

args

函数参数。

code

函数代码。

返回值

以字符串形式返回唯一函数名称,或者出错时返回FALSE

示例

Example #1 Creating an anonymous function with create_function()

您可以使用此函数(例如)根据运行时收集的信息创建函数:

代码语言:javascript
复制
<?php
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
?>

或者,也许有一个通用处理函数可以将一组操作应用于参数列表:

Example #2 Making a general processing function with create_function()

代码语言:javascript
复制
<?php
function process($var1, $var2, $farr)
{
    foreach ($farr as $f) {
        echo $f($var1, $var2) . "\n";
    }
}

// create a bunch of math functions
$f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}';
$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);";
$f3 = 'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }';
$farr = array(
    create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),
    create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),
    create_function('$a,$b', $f1),
    create_function('$a,$b', $f2),
    create_function('$a,$b', $f3)
    );

echo "\nUsing the first array of anonymous functions\n";
echo "parameters: 2.3445, M_PI\n";
process(2.3445, M_PI, $farr);

// now make a bunch of string processing functions
$garr = array(
    create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '.
    'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'),
    create_function('$a,$b', '; return "CRCs: " . crc32($a) . ", ".crc32($b);'),
    create_function('$a,$b', '; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";')
    );
echo "\nUsing the second array of anonymous functions\n";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
?>

上面的例子将输出:

代码语言:javascript
复制
Using the first array of anonymous functions
parameters: 2.3445, M_PI
some trig: -1.6291725057799
a hypotenuse: 3.9199852871011
b*a^2 = 4.8103313314525
min(b^2+a, a^2,b) = 8.6382729035898
ln(a)/b = 0.27122299212594

Using the second array of anonymous functions
** "Twas the night" and "Twas brilling and the slithy toves"
** Look the same to me! (looking at the first 3 chars)
CRCs: -725381282, 342550513
similar(a,b) = 11(45.833333333333%)

但是对于lambda样式(匿名)函数最常见的用法是创建回调函数,例如使用array_walk()或usort()时,

Example #3 Using anonymous functions as callback functions

代码语言:javascript
复制
<?php
$av = array("the ", "a ", "that ", "this ");
array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));
print_r($av);
?>

上面的例子将输出:

代码语言:javascript
复制
Array
(
  [0] => the mango
  [1] => a mango
  [2] => that mango
  [3] => this mango
)

从较短到较长的顺序排列的字符串数组

代码语言:javascript
复制
<?php

$sv = array("small", "larger", "a big string", "it is a string thing");
print_r($sv);

?>

上面的例子将输出:

代码语言:javascript
复制
Array
(
  [0] => small
  [1] => larger
  [2] => a big string
  [3] => it is a string thing
)

从较长到较短的排序

代码语言:javascript
复制
<?php

usort($sv, create_function('$a,$b','return strlen($b) - strlen($a);'));
print_r($sv);

?>

上面的例子将输出:

代码语言:javascript
复制
Array
(
  [0] => it is a string thing
  [1] => a big string
  [2] => larger
  [3] => small
)

另请参阅

  • Anonymous functions

← call_user_func

forward_static_call_array →

扫码关注腾讯云开发者

领取腾讯云代金券