3v4l.org

run code in 300+ PHP versions simultaneously
<?php // オブジェクトの不要なプロパティを削除 function removeUnwantedProperties(mixed $element): mixed { if (is_array($element)) { foreach ($element as $key => $val) { if ($key !== "\0*\0" && is_object($val)) { $element[$key] = removeUnwantedProperties($val); } } } elseif (is_object($element)) { $rc = new ReflectionClass($element); $properties = $rc->getProperties(); foreach ($properties as $property) { if (!filterProperties($property)) { unset($element->{$property->getName()}); } else { $getterName = 'get' . ucfirst($property->getName()); if (method_exists($element, $getterName)) { $element->{$property->getName()} = removeUnwantedProperties($element->$getterName()); } else { die('throw'); } } } } return $element; } function filterProperties($property) { $attributes = $property->getAttributes(); foreach ($attributes as $attribute) { if ($attribute->getName() === NoOutput::class) { return false; } } return true; } class Test1 { public $x; // protected だとエラーになる #[NoOutput] public $y; public function __construct() { $this->x = "X"; $this->y = "Y"; } public function getX() { return $this->x; } public function getY() { return $this->y; } } $t1 = new Test1(); $object = removeUnwantedProperties($t1); print_r($object);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ps79d
function name:  (null)
number of ops:  11
compiled vars:  !0 = $t1, !1 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   NEW                                              $2      'Test1'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   68     3        INIT_FCALL                                               'removeunwantedproperties'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !1, $5
   69     7        INIT_FCALL                                               'print_r'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                                 
         10      > RETURN                                                   1

Function removeunwantedproperties:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 19
Branch analysis from position: 3
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 17
Branch analysis from position: 5
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 16
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 16
Branch analysis from position: 10
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 64
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 29, Position 2 = 63
Branch analysis from position: 29
2 jumps found. (Code = 78) Position 1 = 30, Position 2 = 63
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 39
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 61
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
Branch analysis from position: 61
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 63
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 63
Branch analysis from position: 64
filename:       /in/ps79d
function name:  removeUnwantedProperties
number of ops:  67
compiled vars:  !0 = $element, !1 = $val, !2 = $key, !3 = $rc, !4 = $properties, !5 = $property, !6 = $getterName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
    6     1        TYPE_CHECK                                  128          !0
          2      > JMPZ                                                     ~7, ->19
    7     3    > > FE_RESET_R                                       $8      !0, ->17
          4    > > FE_FETCH_R                                       ~9      $8, !1, ->17
          5    >   ASSIGN                                                   !2, ~9
    8     6        IS_NOT_IDENTICAL                                 ~11     !2, '%00%2A%00'
          7      > JMPZ_EX                                          ~11     ~11, ->10
          8    >   TYPE_CHECK                                  256  ~12     !1
          9        BOOL                                             ~11     ~12
         10    > > JMPZ                                                     ~11, ->16
    9    11    >   INIT_FCALL_BY_NAME                                       'removeUnwantedProperties'
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0  $14     
         14        ASSIGN_DIM                                               !0, !2
         15        OP_DATA                                                  $14
    7    16    > > JMP                                                      ->4
         17    >   FE_FREE                                                  $8
    6    18      > JMP                                                      ->64
   12    19    >   TYPE_CHECK                                  256          !0
         20      > JMPZ                                                     ~15, ->64
   13    21    >   NEW                                              $16     'ReflectionClass'
         22        SEND_VAR_EX                                              !0
         23        DO_FCALL                                      0          
         24        ASSIGN                                                   !3, $16
   14    25        INIT_METHOD_CALL                                         !3, 'getProperties'
         26        DO_FCALL                                      0  $19     
         27        ASSIGN                                                   !4, $19
   15    28      > FE_RESET_R                                       $21     !4, ->63
         29    > > FE_FETCH_R                                               $21, !5, ->63
   16    30    >   INIT_FCALL_BY_NAME                                       'filterProperties'
         31        SEND_VAR_EX                                              !5
         32        DO_FCALL                                      0  $22     
         33        BOOL_NOT                                         ~23     $22
         34      > JMPZ                                                     ~23, ->39
   17    35    >   INIT_METHOD_CALL                                         !5, 'getName'
         36        DO_FCALL                                      0  $24     
         37        UNSET_OBJ                                                !0, $24
   16    38      > JMP                                                      ->62
   19    39    >   INIT_FCALL                                               'ucfirst'
         40        INIT_METHOD_CALL                                         !5, 'getName'
         41        DO_FCALL                                      0  $25     
         42        SEND_VAR                                                 $25
         43        DO_ICALL                                         $26     
         44        CONCAT                                           ~27     'get', $26
         45        ASSIGN                                                   !6, ~27
   20    46        INIT_FCALL                                               'method_exists'
         47        SEND_VAR                                                 !0
         48        SEND_VAR                                                 !6
         49        DO_ICALL                                         $29     
         50      > JMPZ                                                     $29, ->61
   21    51    >   INIT_METHOD_CALL                                         !5, 'getName'
         52        DO_FCALL                                      0  $30     
         53        INIT_FCALL_BY_NAME                                       'removeUnwantedProperties'
         54        INIT_METHOD_CALL                                         !0, !6
         55        DO_FCALL                                      0  $32     
         56        SEND_VAR_NO_REF_EX                                       $32
         57        DO_FCALL                                      0  $33     
         58        ASSIGN_OBJ                                               !0, $30
         59        OP_DATA                                                  $33
   20    60      > JMP                                                      ->62
   23    61    > > EXIT                                                     'throw'
   15    62    > > JMP                                                      ->29
         63    >   FE_FREE                                                  $21
   29    64    > > RETURN                                                   !0
   30    65*       VERIFY_RETURN_TYPE                                       
         66*     > RETURN                                                   null

End of function removeunwantedproperties

Function filterproperties:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 13
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/ps79d
function name:  filterProperties
number of ops:  16
compiled vars:  !0 = $property, !1 = $attributes, !2 = $attribute
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
   34     1        INIT_METHOD_CALL                                         !0, 'getAttributes'
          2        DO_FCALL                                      0  $3      
          3        ASSIGN                                                   !1, $3
   35     4      > FE_RESET_R                                       $5      !1, ->13
          5    > > FE_FETCH_R                                               $5, !2, ->13
   36     6    >   INIT_METHOD_CALL                                         !2, 'getName'
          7        DO_FCALL                                      0  $6      
          8        IS_IDENTICAL                                             $6, 'NoOutput'
          9      > JMPZ                                                     ~7, ->12
   37    10    >   FE_FREE                                                  $5
         11      > RETURN                                                   <false>
   35    12    > > JMP                                                      ->5
         13    >   FE_FREE                                                  $5
   40    14      > RETURN                                                   <true>
   41    15*     > RETURN                                                   null

End of function filterproperties

Class Test1:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ps79d
function name:  __construct
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   ASSIGN_OBJ                                               'x'
          1        OP_DATA                                                  'X'
   53     2        ASSIGN_OBJ                                               'y'
          3        OP_DATA                                                  'Y'
   54     4      > RETURN                                                   null

End of function __construct

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

End of function getx

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

End of function gety

End of class Test1.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
191.32 ms | 1022 KiB | 17 Q