3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Derp; abstract class Enum { protected $value; protected static $possibleValues; protected static function getPossibleValues() { if (static::$possibleValues) { return static::$possibleValues; } $reflectionClass = new \ReflectionClass(get_called_class()); static::$possibleValues = $reflectionClass->getConstants(); return static::$possibleValues; } /** * @param $name * @param $arguments * @return static * @throws \Exception */ public static function __callStatic($name, $arguments) { $possibleValues = static::getPossibleValues(); if (!array_key_exists($name, $possibleValues)) { throw new \Exception; } if (count($arguments) > 0) { throw new \Exception; } return self::createInstance($name, $possibleValues[$name]); } /** * @param $value * @return static */ protected static function createInstance($value) { $enum = new static; $enum->value = $value; return $enum; } protected function getValue() { return $this->value; } public function equals(Enum $otherValue) { return get_class($otherValue) === get_called_class() && $otherValue->getValue() === $this->getValue(); } public static function all() { return array_map( function($value) { return static::createInstance($value); }, static::getPossibleValues() ); } } class Day extends Enum { const MONDAY = 'monday'; const FRIDAY = 'friday'; } $monday = Day::MONDAY(); var_dump($monday); var_dump($monday->equals(Day::FRIDAY())); var_dump(Day::all());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  (null)
number of ops:  20
compiled vars:  !0 = $monday
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   INIT_STATIC_METHOD_CALL                                  'Derp%5CDay', 'MONDAY'
          1        DO_FCALL                                      0  $1      
          2        ASSIGN                                                   !0, $1
   85     3        INIT_NS_FCALL_BY_NAME                                    'Derp%5Cvar_dump'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0          
   86     6        INIT_NS_FCALL_BY_NAME                                    'Derp%5Cvar_dump'
          7        INIT_METHOD_CALL                                         !0, 'equals'
          8        INIT_STATIC_METHOD_CALL                                  'Derp%5CDay', 'FRIDAY'
          9        DO_FCALL                                      0  $4      
         10        SEND_VAR_NO_REF_EX                                       $4
         11        DO_FCALL                                      0  $5      
         12        SEND_VAR_NO_REF_EX                                       $5
         13        DO_FCALL                                      0          
   87    14        INIT_NS_FCALL_BY_NAME                                    'Derp%5Cvar_dump'
         15        INIT_STATIC_METHOD_CALL                                  'Derp%5CDay', 'all'
         16        DO_FCALL                                      0  $7      
         17        SEND_VAR_NO_REF_EX                                       $7
         18        DO_FCALL                                      0          
         19      > RETURN                                                   1

Function %00derp%5C%7Bclosure%7D%2Fin%2Fou19Z%3A67%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  Derp\{closure}
number of ops:  6
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   RECV                                             !0      
   68     1        INIT_STATIC_METHOD_CALL                                  'createInstance'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4      > RETURN                                                   $1
   69     5*     > RETURN                                                   null

End of function %00derp%5C%7Bclosure%7D%2Fin%2Fou19Z%3A67%240

Class Derp\Enum:
Function getpossiblevalues:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 4
Branch analysis from position: 2
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  getPossibleValues
number of ops:  17
compiled vars:  !0 = $reflectionClass
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   FETCH_STATIC_PROP_R          unknown             ~1      'possibleValues'
          1      > JMPZ                                                     ~1, ->4
   14     2    >   FETCH_STATIC_PROP_R          unknown             ~2      'possibleValues'
          3      > RETURN                                                   ~2
   17     4    >   NEW                                              $3      'ReflectionClass'
          5        INIT_NS_FCALL_BY_NAME                                    'Derp%5Cget_called_class'
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR_NO_REF_EX                                       $4
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !0, $3
   18    10        INIT_METHOD_CALL                                         !0, 'getConstants'
         11        DO_FCALL                                      0  $8      
         12        ASSIGN_STATIC_PROP                                       'possibleValues'
         13        OP_DATA                                                  $8
   20    14        FETCH_STATIC_PROP_R          unknown             ~9      'possibleValues'
         15      > RETURN                                                   ~9
   21    16*     > RETURN                                                   null

End of function getpossiblevalues

Function __callstatic:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 22
Branch analysis from position: 19
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  __callStatic
number of ops:  30
compiled vars:  !0 = $name, !1 = $arguments, !2 = $possibleValues
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   31     2        INIT_STATIC_METHOD_CALL                                  'getPossibleValues'
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !2, $3
   32     5        INIT_NS_FCALL_BY_NAME                                    'Derp%5Carray_key_exists'
          6        SEND_VAR_EX                                              !0
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0  $5      
          9        BOOL_NOT                                         ~6      $5
         10      > JMPZ                                                     ~6, ->14
   33    11    >   NEW                                              $7      'Exception'
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $7
   36    14    >   INIT_NS_FCALL_BY_NAME                                    'Derp%5Ccount'
         15        SEND_VAR_EX                                              !1
         16        DO_FCALL                                      0  $9      
         17        IS_SMALLER                                               0, $9
         18      > JMPZ                                                     ~10, ->22
   37    19    >   NEW                                              $11     'Exception'
         20        DO_FCALL                                      0          
         21      > THROW                                         0          $11
   40    22    >   INIT_STATIC_METHOD_CALL                                  'createInstance'
         23        SEND_VAR_EX                                              !0
         24        CHECK_FUNC_ARG                                           
         25        FETCH_DIM_FUNC_ARG                               $13     !2, !0
         26        SEND_FUNC_ARG                                            $13
         27        DO_FCALL                                      0  $14     
         28      > RETURN                                                   $14
   41    29*     > RETURN                                                   null

End of function __callstatic

Function createinstance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  createInstance
number of ops:  8
compiled vars:  !0 = $value, !1 = $enum
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
   49     1        NEW                          static              $2      
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $2
   50     4        ASSIGN_OBJ                                               !1, 'value'
          5        OP_DATA                                                  !0
   51     6      > RETURN                                                   !1
   52     7*     > RETURN                                                   null

End of function createinstance

Function getvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  getValue
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   FETCH_OBJ_R                                      ~0      'value'
          1      > RETURN                                                   ~0
   57     2*     > RETURN                                                   null

End of function getvalue

Function equals:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/ou19Z
function name:  equals
number of ops:  16
compiled vars:  !0 = $otherValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   61     1        INIT_NS_FCALL_BY_NAME                                    'Derp%5Cget_class'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4        INIT_NS_FCALL_BY_NAME                                    'Derp%5Cget_called_class'
          5        DO_FCALL                                      0  $2      
          6        IS_IDENTICAL                                     ~3      $1, $2
          7      > JMPZ_EX                                          ~3      ~3, ->14
          8    >   INIT_METHOD_CALL                                         !0, 'getValue'
          9        DO_FCALL                                      0  $4      
         10        INIT_METHOD_CALL                                         'getValue'
         11        DO_FCALL                                      0  $5      
         12        IS_IDENTICAL                                     ~6      $4, $5
         13        BOOL                                             ~3      ~6
         14    > > RETURN                                                   ~3
   62    15*     > RETURN                                                   null

End of function equals

Function all:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  all
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   INIT_NS_FCALL_BY_NAME                                    'Derp%5Carray_map'
   67     1        DECLARE_LAMBDA_FUNCTION                                  '%00derp%5C%7Bclosure%7D%2Fin%2Fou19Z%3A67%240'
   69     2        SEND_VAL_EX                                              ~0
   70     3        INIT_STATIC_METHOD_CALL                                  'getPossibleValues'
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR_NO_REF_EX                                       $1
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
   72     8*     > RETURN                                                   null

End of function all

End of class Derp\Enum.

Class Derp\Day:
Function getpossiblevalues:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 4
Branch analysis from position: 2
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  getPossibleValues
number of ops:  17
compiled vars:  !0 = $reflectionClass
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   FETCH_STATIC_PROP_R          unknown             ~1      'possibleValues'
          1      > JMPZ                                                     ~1, ->4
   14     2    >   FETCH_STATIC_PROP_R          unknown             ~2      'possibleValues'
          3      > RETURN                                                   ~2
   17     4    >   NEW                                              $3      'ReflectionClass'
          5        INIT_NS_FCALL_BY_NAME                                    'Derp%5Cget_called_class'
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR_NO_REF_EX                                       $4
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !0, $3
   18    10        INIT_METHOD_CALL                                         !0, 'getConstants'
         11        DO_FCALL                                      0  $8      
         12        ASSIGN_STATIC_PROP                                       'possibleValues'
         13        OP_DATA                                                  $8
   20    14        FETCH_STATIC_PROP_R          unknown             ~9      'possibleValues'
         15      > RETURN                                                   ~9
   21    16*     > RETURN                                                   null

End of function getpossiblevalues

Function __callstatic:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 22
Branch analysis from position: 19
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  __callStatic
number of ops:  30
compiled vars:  !0 = $name, !1 = $arguments, !2 = $possibleValues
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   31     2        INIT_STATIC_METHOD_CALL                                  'getPossibleValues'
          3        DO_FCALL                                      0  $3      
          4        ASSIGN                                                   !2, $3
   32     5        INIT_NS_FCALL_BY_NAME                                    'Derp%5Carray_key_exists'
          6        SEND_VAR_EX                                              !0
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0  $5      
          9        BOOL_NOT                                         ~6      $5
         10      > JMPZ                                                     ~6, ->14
   33    11    >   NEW                                              $7      'Exception'
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $7
   36    14    >   INIT_NS_FCALL_BY_NAME                                    'Derp%5Ccount'
         15        SEND_VAR_EX                                              !1
         16        DO_FCALL                                      0  $9      
         17        IS_SMALLER                                               0, $9
         18      > JMPZ                                                     ~10, ->22
   37    19    >   NEW                                              $11     'Exception'
         20        DO_FCALL                                      0          
         21      > THROW                                         0          $11
   40    22    >   INIT_STATIC_METHOD_CALL                                  'createInstance'
         23        SEND_VAR_EX                                              !0
         24        CHECK_FUNC_ARG                                           
         25        FETCH_DIM_FUNC_ARG                               $13     !2, !0
         26        SEND_FUNC_ARG                                            $13
         27        DO_FCALL                                      0  $14     
         28      > RETURN                                                   $14
   41    29*     > RETURN                                                   null

End of function __callstatic

Function createinstance:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  createInstance
number of ops:  8
compiled vars:  !0 = $value, !1 = $enum
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
   49     1        NEW                          static              $2      
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $2
   50     4        ASSIGN_OBJ                                               !1, 'value'
          5        OP_DATA                                                  !0
   51     6      > RETURN                                                   !1
   52     7*     > RETURN                                                   null

End of function createinstance

Function getvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  getValue
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   FETCH_OBJ_R                                      ~0      'value'
          1      > RETURN                                                   ~0
   57     2*     > RETURN                                                   null

End of function getvalue

Function equals:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/ou19Z
function name:  equals
number of ops:  16
compiled vars:  !0 = $otherValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   61     1        INIT_NS_FCALL_BY_NAME                                    'Derp%5Cget_class'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4        INIT_NS_FCALL_BY_NAME                                    'Derp%5Cget_called_class'
          5        DO_FCALL                                      0  $2      
          6        IS_IDENTICAL                                     ~3      $1, $2
          7      > JMPZ_EX                                          ~3      ~3, ->14
          8    >   INIT_METHOD_CALL                                         !0, 'getValue'
          9        DO_FCALL                                      0  $4      
         10        INIT_METHOD_CALL                                         'getValue'
         11        DO_FCALL                                      0  $5      
         12        IS_IDENTICAL                                     ~6      $4, $5
         13        BOOL                                             ~3      ~6
         14    > > RETURN                                                   ~3
   62    15*     > RETURN                                                   null

End of function equals

Function all:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ou19Z
function name:  all
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   INIT_NS_FCALL_BY_NAME                                    'Derp%5Carray_map'
   67     1        DECLARE_LAMBDA_FUNCTION                                  '%00derp%5C%7Bclosure%7D%2Fin%2Fou19Z%3A67%240'
   69     2        SEND_VAL_EX                                              ~0
   70     3        INIT_STATIC_METHOD_CALL                                  'getPossibleValues'
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR_NO_REF_EX                                       $1
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
   72     8*     > RETURN                                                   null

End of function all

End of class Derp\Day.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
165.6 ms | 1416 KiB | 25 Q