3v4l.org

run code in 300+ PHP versions simultaneously
<?php enum ProductCategories: string { case TSHIRT = 't_shirt'; case SHOES = 'shoes'; } abstract class Base { protected static array $categoryMappings; public static function getLocalCategoryId(ProductCategories $category): ?string { // Use $category->value to access the scalar value of the enum return static::$categoryMappings[$category->value] ?? null; } public static function getCategoryFromLocalId(string $categoryId): ?ProductCategories { // Flip the array to find the original enum based on categoryId $inverted = array_flip(static::$categoryMappings); if (array_key_exists($categoryId, $inverted)) { // Use ProductCategories::from to create enum from the string value return ProductCategories::from($inverted[$categoryId]); } return null; } abstract public function getByCategory(ProductCategories $category); } class A extends Base { protected static array $categoryMappings = [ ProductCategories::TSHIRT->value => '1', ProductCategories::SHOES->value => '2', ]; public function getByCategory(ProductCategories $category) { $localCategory = static::getLocalCategoryId($category); // Makes API call and returns results return $localCategory; } } class B extends Base { protected static array $categoryMappings = [ ProductCategories::TSHIRT->value => 't-shirt', ProductCategories::SHOES->value => 'mens-shoes', ]; public function getByCategory(ProductCategories $category) { $localCategory = static::getLocalCategoryId($category); // Makes API call and returns results return $localCategory; } } // Test cases echo A::getLocalCategoryId(ProductCategories::TSHIRT); // 1 echo "\n"; echo B::getLocalCategoryId(ProductCategories::TSHIRT); // t-shirt echo "\n"; echo A::getCategoryFromLocalId('1') === B::getCategoryFromLocalId('t-shirt') ? 'Same' : 'Not same'; echo "\n"; echo A::getLocalCategoryId(ProductCategories::SHOES); // 2 echo "\n"; echo B::getLocalCategoryId(ProductCategories::SHOES); // mens-shoes echo "\n"; echo "\n\n\n"; $aClass = new A(); echo $aClass->getByCategory(ProductCategories::TSHIRT); echo "\n"; $bClass = new B(); echo $bClass->getByCategory(ProductCategories::TSHIRT);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 23
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7umSN
function name:  (null)
number of ops:  57
compiled vars:  !0 = $aClass, !1 = $bClass
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CLASS                                            'productcategories'
   65     1        INIT_STATIC_METHOD_CALL                                  'A', 'getLocalCategoryId'
          2        FETCH_CLASS_CONSTANT                             ~2      'ProductCategories', 'TSHIRT'
          3        SEND_VAL                                                 ~2
          4        DO_FCALL                                      0  $3      
          5        ECHO                                                     $3
   66     6        ECHO                                                     '%0A'
   67     7        INIT_STATIC_METHOD_CALL                                  'B', 'getLocalCategoryId'
          8        FETCH_CLASS_CONSTANT                             ~4      'ProductCategories', 'TSHIRT'
          9        SEND_VAL                                                 ~4
         10        DO_FCALL                                      0  $5      
         11        ECHO                                                     $5
   68    12        ECHO                                                     '%0A'
   70    13        INIT_STATIC_METHOD_CALL                                  'A', 'getCategoryFromLocalId'
         14        SEND_VAL                                                 '1'
         15        DO_FCALL                                      0  $6      
         16        INIT_STATIC_METHOD_CALL                                  'B', 'getCategoryFromLocalId'
         17        SEND_VAL                                                 't-shirt'
         18        DO_FCALL                                      0  $7      
         19        IS_IDENTICAL                                             $6, $7
         20      > JMPZ                                                     ~8, ->23
         21    >   QM_ASSIGN                                        ~9      'Same'
         22      > JMP                                                      ->24
         23    >   QM_ASSIGN                                        ~9      'Not+same'
         24    >   ECHO                                                     ~9
   71    25        ECHO                                                     '%0A'
   73    26        INIT_STATIC_METHOD_CALL                                  'A', 'getLocalCategoryId'
         27        FETCH_CLASS_CONSTANT                             ~10     'ProductCategories', 'SHOES'
         28        SEND_VAL                                                 ~10
         29        DO_FCALL                                      0  $11     
         30        ECHO                                                     $11
   74    31        ECHO                                                     '%0A'
   75    32        INIT_STATIC_METHOD_CALL                                  'B', 'getLocalCategoryId'
         33        FETCH_CLASS_CONSTANT                             ~12     'ProductCategories', 'SHOES'
         34        SEND_VAL                                                 ~12
         35        DO_FCALL                                      0  $13     
         36        ECHO                                                     $13
   76    37        ECHO                                                     '%0A'
   78    38        ECHO                                                     '%0A%0A%0A'
   80    39        NEW                                              $14     'A'
         40        DO_FCALL                                      0          
         41        ASSIGN                                                   !0, $14
   81    42        INIT_METHOD_CALL                                         !0, 'getByCategory'
         43        FETCH_CLASS_CONSTANT                             ~17     'ProductCategories', 'TSHIRT'
         44        SEND_VAL_EX                                              ~17
         45        DO_FCALL                                      0  $18     
         46        ECHO                                                     $18
   83    47        ECHO                                                     '%0A'
   85    48        NEW                                              $19     'B'
         49        DO_FCALL                                      0          
         50        ASSIGN                                                   !1, $19
   86    51        INIT_METHOD_CALL                                         !1, 'getByCategory'
         52        FETCH_CLASS_CONSTANT                             ~22     'ProductCategories', 'TSHIRT'
         53        SEND_VAL_EX                                              ~22
         54        DO_FCALL                                      0  $23     
         55        ECHO                                                     $23
         56      > RETURN                                                   1

Class ProductCategories: [no user functions]
Class Base:
Function getlocalcategoryid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7umSN
function name:  getLocalCategoryId
number of ops:  10
compiled vars:  !0 = $category
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   16     1        FETCH_OBJ_R                                      ~2      !0, 'value'
          2        FETCH_STATIC_PROP_IS                             ~1      'categoryMappings'
          3        FETCH_DIM_IS                                     ~3      ~1, ~2
          4        COALESCE                                         ~4      ~3
          5        QM_ASSIGN                                        ~4      null
          6        VERIFY_RETURN_TYPE                                       ~4
          7      > RETURN                                                   ~4
   17     8*       VERIFY_RETURN_TYPE                                       
          9*     > RETURN                                                   null

End of function getlocalcategoryid

Function getcategoryfromlocalid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7umSN
function name:  getCategoryFromLocalId
number of ops:  18
compiled vars:  !0 = $categoryId, !1 = $inverted
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   22     1        INIT_FCALL                                               'array_flip'
          2        FETCH_STATIC_PROP_R          global lock         ~2      'categoryMappings'
          3        SEND_VAL                                                 ~2
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !1, $3
   24     6        ARRAY_KEY_EXISTS                                         !0, !1
          7      > JMPZ                                                     ~5, ->15
   26     8    >   INIT_STATIC_METHOD_CALL                                  'ProductCategories', 'from'
          9        CHECK_FUNC_ARG                                           
         10        FETCH_DIM_FUNC_ARG                               $6      !1, !0
         11        SEND_FUNC_ARG                                            $6
         12        DO_FCALL                                      0  $7      
         13        VERIFY_RETURN_TYPE                                       $7
         14      > RETURN                                                   $7
   29    15    > > RETURN                                                   null
   30    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of function getcategoryfromlocalid

Function getbycategory:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7umSN
function name:  getByCategory
number of ops:  2
compiled vars:  !0 = $category
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function getbycategory

End of class Base.

Class A:
Function getbycategory:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7umSN
function name:  getByCategory
number of ops:  7
compiled vars:  !0 = $category, !1 = $localCategory
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
   44     1        INIT_STATIC_METHOD_CALL                                  'getLocalCategoryId'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !1, $2
   46     5      > RETURN                                                   !1
   47     6*     > RETURN                                                   null

End of function getbycategory

Function getlocalcategoryid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7umSN
function name:  getLocalCategoryId
number of ops:  10
compiled vars:  !0 = $category
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   16     1        FETCH_OBJ_R                                      ~2      !0, 'value'
          2        FETCH_STATIC_PROP_IS                             ~1      'categoryMappings'
          3        FETCH_DIM_IS                                     ~3      ~1, ~2
          4        COALESCE                                         ~4      ~3
          5        QM_ASSIGN                                        ~4      null
          6        VERIFY_RETURN_TYPE                                       ~4
          7      > RETURN                                                   ~4
   17     8*       VERIFY_RETURN_TYPE                                       
          9*     > RETURN                                                   null

End of function getlocalcategoryid

Function getcategoryfromlocalid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7umSN
function name:  getCategoryFromLocalId
number of ops:  18
compiled vars:  !0 = $categoryId, !1 = $inverted
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   22     1        INIT_FCALL                                               'array_flip'
          2        FETCH_STATIC_PROP_R          global lock         ~2      'categoryMappings'
          3        SEND_VAL                                                 ~2
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !1, $3
   24     6        ARRAY_KEY_EXISTS                                         !0, !1
          7      > JMPZ                                                     ~5, ->15
   26     8    >   INIT_STATIC_METHOD_CALL                                  'ProductCategories', 'from'
          9        CHECK_FUNC_ARG                                           
         10        FETCH_DIM_FUNC_ARG                               $6      !1, !0
         11        SEND_FUNC_ARG                                            $6
         12        DO_FCALL                                      0  $7      
         13        VERIFY_RETURN_TYPE                                       $7
         14      > RETURN                                                   $7
   29    15    > > RETURN                                                   null
   30    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of function getcategoryfromlocalid

End of class A.

Class B:
Function getbycategory:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7umSN
function name:  getByCategory
number of ops:  7
compiled vars:  !0 = $category, !1 = $localCategory
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
   58     1        INIT_STATIC_METHOD_CALL                                  'getLocalCategoryId'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !1, $2
   60     5      > RETURN                                                   !1
   61     6*     > RETURN                                                   null

End of function getbycategory

Function getlocalcategoryid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7umSN
function name:  getLocalCategoryId
number of ops:  10
compiled vars:  !0 = $category
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   16     1        FETCH_OBJ_R                                      ~2      !0, 'value'
          2        FETCH_STATIC_PROP_IS                             ~1      'categoryMappings'
          3        FETCH_DIM_IS                                     ~3      ~1, ~2
          4        COALESCE                                         ~4      ~3
          5        QM_ASSIGN                                        ~4      null
          6        VERIFY_RETURN_TYPE                                       ~4
          7      > RETURN                                                   ~4
   17     8*       VERIFY_RETURN_TYPE                                       
          9*     > RETURN                                                   null

End of function getlocalcategoryid

Function getcategoryfromlocalid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7umSN
function name:  getCategoryFromLocalId
number of ops:  18
compiled vars:  !0 = $categoryId, !1 = $inverted
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   22     1        INIT_FCALL                                               'array_flip'
          2        FETCH_STATIC_PROP_R          global lock         ~2      'categoryMappings'
          3        SEND_VAL                                                 ~2
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !1, $3
   24     6        ARRAY_KEY_EXISTS                                         !0, !1
          7      > JMPZ                                                     ~5, ->15
   26     8    >   INIT_STATIC_METHOD_CALL                                  'ProductCategories', 'from'
          9        CHECK_FUNC_ARG                                           
         10        FETCH_DIM_FUNC_ARG                               $6      !1, !0
         11        SEND_FUNC_ARG                                            $6
         12        DO_FCALL                                      0  $7      
         13        VERIFY_RETURN_TYPE                                       $7
         14      > RETURN                                                   $7
   29    15    > > RETURN                                                   null
   30    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of function getcategoryfromlocalid

End of class B.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.28 ms | 1449 KiB | 14 Q