-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_function.php
More file actions
78 lines (69 loc) · 1.78 KB
/
create_function.php
File metadata and controls
78 lines (69 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/*
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. This file is offered as-is,
without any warranty.
*/
/**
* Polyfill of create_function()
*
* @copylight 2020 Friends of PHP5
* @author USAMI Kenta <tadsan@zonu.me>
* @license FSFAP
*/
namespace Php5Friends
{
/**
* Create a Closure from string
*
* NOTICE: Do not input that passed from user request
*
* @see https://www.php.net/create_function
* @param string $args
* @param string $code
* @return \Closure
*/
function create_closure($args, $code)
{
return eval("return function ({$args}) { {$code} };");
}
/**
* Create an anonymous (lambda-style) function, without error
*
* NOTICE: Do not input that passed from user request
*
* @see https://www.php.net/create_function
* @param string $args
* @param string $code
* @phpstan-return callable-string
* @psalm-return callable-string
*/
function create_function($args, $code)
{
if (PHP_MAJOR_VERSION <= 7) {
return @\create_function($args, $code);
}
static $i;
$namespace = __NAMESPACE__;
do {
$i++;
$name = "__{$namespace}_lambda_{$i}";
} while (\function_exists($name));
eval("function {$name}({$args}) { {$code} }");
return $name;
}
}
namespace {
if (!function_exists('create_function')) {
/**
* @param string $args
* @param string $code
* @return string
*/
function create_function($args, $code)
{
return Php5Friends\create_function($args, $code);
}
}
}