update page now
PHP 8.1.34 Released!

ReflectionFunction::isAnonymous

(PHP 8 >= 8.2.0)

ReflectionFunction::isAnonymousVérifie si la fonction est anonyme

Description

public ReflectionFunction::isAnonymous(): bool

Vérifie si la fonction est anonyme.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Renvoie true si la fonction est anonyme, sinon false.

Exemples

Exemple #1 Exemple de ReflectionFunction::isAnonymous()

<?php

$rf
= new ReflectionFunction(function() {});
var_dump($rf->isAnonymous());

$rf = new ReflectionFunction('strlen');
var_dump($rf->isAnonymous());
?>

L'exemple ci-dessus va afficher :

bool(true)
bool(false)

Voir aussi

add a note

User Contributed Notes 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) { /* ... */ }

?>
To Top

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant