PHP 8.5.8 Released!

ReflectionFunction::isAnonymous

(PHP 8 >= 8.2.0)

ReflectionFunction::isAnonymousChecks if a function is anonymous

说明

public function ReflectionFunction::isAnonymous(): bool

Checks if a function is anonymous.

参数

此函数没有参数。

返回值

Returns true if the function is anonymous, otherwise false .

示例

示例 #1 ReflectionFunction::isAnonymous() example

<?php
$rf = new ReflectionFunction(function() {});
var_dump($rf->isAnonymous());
$rf = new ReflectionFunction('strlen');
var_dump($rf->isAnonymous());
?>

以上示例会输出:

bool(true)
bool(false)

发现了问题?

了解如何改进此页面提交拉取请求报告一个错误
+添加备注

用户贡献的备注 2 notes

up
3
nicolasgrekas at php dot net
3 years ago
Closures can be either anonymous or not.
Here is an anonymous closure:
$c1 = function () {};
And here is a *non* anonymous closure:
$c2 = Closure::fromCallable(['Foo', 'bar']);
ReflectionFunction::isAnonymous() returns true for $c1 and false for $c2.
Before PHP 8.2, one had to do this check to decide between both:
$r = new \ReflectionFunction($c1);
$isAnonymous = false !== strpos($r->name, '{closure}');
ReflectionFunction::isAnonymous() makes it easier to check.
up
-1
Taufik Nurrohman
3 years ago
You know that anonymous function is just an instance of class `Closure` so this would be equivalent to check whether a variable is an anonymous function or not:
<?php
$test = function () {};
if (is_callable($test) && is_object($test) && $test instanceof Closure) { /* ... */ }
?>
+添加备注

AltStyle によって変換されたページ (->オリジナル) /