Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance of closure checking

We have an anonymous function in a stdclass object like following code:

$std = new \stdclass();
$std->method = function(){echo "Hi I'm  instance of Closure.";};

echo get_class($std->method); // prints "Closure"

if ($std->method instanceof Closure) {
    echo "Happy Ending."; // This line never gets executed.
}

anonymous function are instance of Closure class, but when I check it with instanceof keyword it doesn't return true. what's the problem?

like image 963
Lost Koder Avatar asked Dec 04 '25 02:12

Lost Koder


1 Answers

Problem is in namespace I had to use \Closure instead of Closure.

like image 91
Lost Koder Avatar answered Dec 06 '25 17:12

Lost Koder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!