Bug #73118 [Opn->Csd]: is_callable callable name reports misleading value for anonymous classes

From: Date: Sat, 11 Feb 2017 23:16:16 +0000
Subject: Bug #73118 [Opn->Csd]: is_callable callable name reports misleading value for anonymous classes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207276@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73118&edit=1

 ID:                 73118
 Updated by:         nikic@php.net
 Reported by:        nihylum at gmail dot com
 Summary:            is_callable callable name reports misleading value
                     for anonymous classes
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            Class/Object related
 Operating System:   any
 PHP Version:        7.0.11
 Block user comment: N
 Private report:     N

 New Comment:

Automatic comment on behalf of as
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7e5cf2aa194a45470efe1fbe590ebff74034df14
Log: Fixed bug #73118


Previous Comments:
------------------------------------------------------------------------
[2016-09-20 05:07:37] nihylum at gmail dot com

Well, i was sure i targeted the correct boxes for expected and actual result (at this bug report
currently interchanged). Sorry, my bad. :/

------------------------------------------------------------------------
[2016-09-20 04:58:59] nihylum at gmail dot com

Description:
------------
Tested on:
- PHP 7.0.8 ( eval.in )
- PHP 7.1.0beta3 via brew (OSX)

Whenever you analyze a callable that is defined by an anonymous class the callable name remains to
be "class@anonymous" instead of the real callable name.

Solution 1:

Modify the callable name construction for anonymous classes to this pattern:
class@anonymous::<method>

Solution 2:

Attach a 4th parameter $resolveAnonymous (that defaults maybe to true, or false for backward
compatibility). If set to true each anonymous class and closures will be automatically resolved to
its object hash enclosed into box brackets as the class name part of the callable name value (the
brackets because of pattern isolation from real class names).

Solution 3 (my favorite):

Solution 1 + 2 combined.

Test script:
---------------
<?php

trait callback {
  public function __invoke($who)
  {
    return 'hello world';
  }
}

class foo { use callback; }

$inline = new class { use callback; };

var_dump(
  is_callable($inline, false, $target),
  $target,
  is_callable([$inline, '__invoke'], false, $target),
  $target,
  is_callable(new foo, false, $target),
  $target
);

Expected result:
----------------
bool(true)
string(15) "class@anonymous"
bool(true)
string(15) "class@anonymous"
bool(true)
string(13) "foo::__invoke"

Actual result:
--------------
Solution 1:

bool(true)
string(15) "class@anonymous::__invoke"
bool(true)
string(15) "class@anonymous::__invoke"
bool(true)
string(13) "foo::__invoke"

Solution 2:

bool(true)
string(15) "[000000003cc480c70000000035210066]::__invoke"
bool(true)
string(15) "[000000003cc480c70000000035210066]::__invoke"
bool(true)
string(13) "foo::__invoke"


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=73118&edit=1


Thread (3 messages)

« previous php.bugs (#207276) next »