3v4l.org

run code in 300+ PHP versions simultaneously
<?php #[Attribute] class BaseAttribute { public function __construct(public readonly string $name) { } } #[Attribute] class ConstantlyNameAttribute extends BaseAttribute { public function __construct() { parent::__construct('Chris'); } } #[ConstantlyNameAttribute] class Thing{} $reflectionClass = new ReflectionClass(Thing::class); // Doesn't work var_dump(getName($reflectionClass->getAttributes(BaseAttribute::class))); // Works var_dump(getName($reflectionClass->getAttributes(BaseAttribute::class, ReflectionAttribute::IS_INSTANCEOF))); // Helper function function getName(array $reflectionAttributes) : ?string { if(!count($reflectionAttributes)){ return null; } $attribute = array_pop($reflectionAttributes); return $attribute->newInstance()->name; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SWTa6
function name:  (null)
number of ops:  24
compiled vars:  !0 = $reflectionClass
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   NEW                                              $1      'ReflectionClass'
          1        SEND_VAL_EX                                              'Thing'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   26     4        INIT_FCALL                                               'var_dump'
          5        INIT_FCALL_BY_NAME                                       'getName'
          6        INIT_METHOD_CALL                                         !0, 'getAttributes'
          7        SEND_VAL_EX                                              'BaseAttribute'
          8        DO_FCALL                                      0  $4      
          9        SEND_VAR_NO_REF_EX                                       $4
         10        DO_FCALL                                      0  $5      
         11        SEND_VAR                                                 $5
         12        DO_ICALL                                                 
   29    13        INIT_FCALL                                               'var_dump'
         14        INIT_FCALL_BY_NAME                                       'getName'
         15        INIT_METHOD_CALL                                         !0, 'getAttributes'
         16        SEND_VAL_EX                                              'BaseAttribute'
         17        SEND_VAL_EX                                              2
         18        DO_FCALL                                      0  $7      
         19        SEND_VAR_NO_REF_EX                                       $7
         20        DO_FCALL                                      0  $8      
         21        SEND_VAR                                                 $8
         22        DO_ICALL                                                 
   40    23      > RETURN                                                   1

Function getname:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SWTa6
function name:  getName
number of ops:  16
compiled vars:  !0 = $reflectionAttributes, !1 = $attribute
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   34     1        COUNT                                            ~2      !0
          2        BOOL_NOT                                         ~3      ~2
          3      > JMPZ                                                     ~3, ->5
   35     4    > > RETURN                                                   null
   38     5    >   INIT_FCALL                                               'array_pop'
          6        SEND_REF                                                 !0
          7        DO_ICALL                                         $4      
          8        ASSIGN                                                   !1, $4
   39     9        INIT_METHOD_CALL                                         !1, 'newInstance'
         10        DO_FCALL                                      0  $6      
         11        FETCH_OBJ_R                                      ~7      $6, 'name'
         12        VERIFY_RETURN_TYPE                                       ~7
         13      > RETURN                                                   ~7
   40    14*       VERIFY_RETURN_TYPE                                       
         15*     > RETURN                                                   null

End of function getname

Class BaseAttribute:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SWTa6
function name:  __construct
number of ops:  4
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'name'
          2        OP_DATA                                                  !0
    8     3      > RETURN                                                   null

End of function __construct

End of class BaseAttribute.

Class ConstantlyNameAttribute:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SWTa6
function name:  __construct
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   INIT_STATIC_METHOD_CALL                                  
          1        SEND_VAL_EX                                              'Chris'
          2        DO_FCALL                                      0          
   17     3      > RETURN                                                   null

End of function __construct

End of class ConstantlyNameAttribute.

Class Thing: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.3 ms | 1011 KiB | 15 Q