|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-10-07 10:27 UTC] acid24 at gmail dot com
Description:
------------
method_exists() has a weird behavior when used on anonymous functions. One would expect that testing the variable that holds the anonymous function and the class of the anonymous function (Closure) to either have or not the __invoke method. Instead method_exists() reports that the variable that holds the anonymous function HAS an __invoke method and the class of the anonymous function HAS NOT an __invoke method. Is this behavior correct? If yes, maybe somebody can explain why. Thank you in advance.
Test script:
---------------
<?php
$lambda = function() {};
var_dump( method_exists( $lambda, '__invoke' ) );
var_dump( method_exists( get_class( $lambda ), '__invoke' ) );
Expected result:
----------------
boolean(false)
boolean(false)
or
boolean(true)
boolean(true)
Actual result:
--------------
boolean(true)
boolean(false)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 19:00:02 2025 UTC |
Thank you for the clear explanation. For anyone interested I found that Reflection gives me the "correct" behavior. <?php $lambda = function() {}; $r = new ReflectionObject( $lambda ); var_dump( $r->hasMethod( '__invoke' ) ); $r = new ReflectionClass( get_class( $lambda ) ); var_dump( $r->hasMethod( '__invoke' ) ); ?> outputs bool(false) bool(false)