3v4l.org

run code in 300+ PHP versions simultaneously
<?php function profile($c, $n = 1000) { $m = 'microtime'; for ($i = $n * is_callable($c), $t = 0;$i--;$w = $m(1), $c(), $t+= ($m(1) - $w) / $n); return $t; } class Foo { protected $foo; protected $bar; protected $baz; } class FooSelfHydratingProxy extends Foo { /** zero-argument constructor since this is just a helper class for hydration */ public function __construct() { } /** * the `Foo` typehint is not part of the interface - here only for the sake of readability/clearness */ public function hydrate($data, Foo $object) { $object->foo = $data['foo']; $object->bar = $data['bar']; $object->baz = $data['baz']; } /** * the `Foo` typehint is not part of the interface - here only for the sake of readability/clearness */ public function extract(Foo $object) { return array('foo' => $object->foo, 'bar' => $object->bar, 'baz' => $object->baz); } } // data $data = array('foo' => 1, 'bar' => 2, 'baz' => 3); // lastguest function dehydrate($obj){ $class = get_class($obj); $exp = var_export($obj,1); return eval('return '.substr($exp,strpos($exp,'(')+1,-1).';'); } function static_hydrate($class,$dry=null){ static $hydrators = array(); $h = '_Hydrator__'.$class; if (empty($hydrators[$class])) { eval("class $h extends $class { static function __hydrate(\$a) { \$o = new $class(); foreach (\$a as \$n=>\$v) \$o->\$n=\$v; return \$o; } } function hydrate_$class(\$o){return $h::__hydrate(\$o);}" ); $hydrators[$class] = true; } return $dry?$h::__hydrate($dry):new $h; } function hydrate($class,$dry=null){ static $hydrators = array(); $h = '_Hydrator__'.$class; if (empty($hydrators[$class])) { eval("class $h extends $class { static function __hydrate(\$a) { \$o = new $class(); foreach (\$a as \$n=>\$v) \$o->\$n=\$v; return \$o; } } function hydrate_$class(\$o){return $h::__hydrate(\$o);}" ); $hydrators[$class] = true; } return $dry?$h::__hydrate($dry):new $h; } $hydrator = new FooSelfHydratingProxy(); echo 'Ocramius: ',profile(function() use ($data,$hydrator) { $hydrated = new Foo(); $hydrator->hydrate($data, $hydrated); })*1E6,PHP_EOL; $hydr = static_hydrate('Foo'); echo 'Lastguest: ',profile(function() use ($data) { hydrate_Foo($data); })*1E6,PHP_EOL; $hydr = static_hydrate('Foo'); echo 'Lastguest: ',profile(function() use ($data,$hydr) { $hydr->__hydrate($data); })*1E6,PHP_EOL; $foo = hydrate_Foo($data); $foo_s = serialize($foo); echo 'Lastguest: ',profile(function() use ($data,$foo_s) { unserialize($foo_s); })*1E6,PHP_EOL;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QK8Wu
function name:  (null)
number of ops:  60
compiled vars:  !0 = $data, !1 = $hydrator, !2 = $hydr, !3 = $foo, !4 = $foo_s
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   ASSIGN                                                   !0, <array>
   90     1        NEW                                              $6      'FooSelfHydratingProxy'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $6
   91     4        ECHO                                                     'Ocramius%3A+'
          5        INIT_FCALL                                               'profile'
          6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FQK8Wu%3A91%240'
          7        BIND_LEXICAL                                             ~9, !0
          8        BIND_LEXICAL                                             ~9, !1
   94     9        SEND_VAL                                                 ~9
         10        DO_FCALL                                      0  $10     
         11        MUL                                              ~11     $10, 1.0e+6
         12        ECHO                                                     ~11
         13        ECHO                                                     '%0A'
   96    14        INIT_FCALL                                               'static_hydrate'
         15        SEND_VAL                                                 'Foo'
         16        DO_FCALL                                      0  $12     
         17        ASSIGN                                                   !2, $12
   97    18        ECHO                                                     'Lastguest%3A+'
         19        INIT_FCALL                                               'profile'
         20        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FQK8Wu%3A97%241'
         21        BIND_LEXICAL                                             ~14, !0
   99    22        SEND_VAL                                                 ~14
         23        DO_FCALL                                      0  $15     
         24        MUL                                              ~16     $15, 1.0e+6
         25        ECHO                                                     ~16
         26        ECHO                                                     '%0A'
  101    27        INIT_FCALL                                               'static_hydrate'
         28        SEND_VAL                                                 'Foo'
         29        DO_FCALL                                      0  $17     
         30        ASSIGN                                                   !2, $17
  102    31        ECHO                                                     'Lastguest%3A+'
         32        INIT_FCALL                                               'profile'
         33        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FQK8Wu%3A102%242'
         34        BIND_LEXICAL                                             ~19, !0
         35        BIND_LEXICAL                                             ~19, !2
  104    36        SEND_VAL                                                 ~19
         37        DO_FCALL                                      0  $20     
         38        MUL                                              ~21     $20, 1.0e+6
         39        ECHO                                                     ~21
         40        ECHO                                                     '%0A'
  106    41        INIT_FCALL_BY_NAME                                       'hydrate_Foo'
         42        SEND_VAR_EX                                              !0
         43        DO_FCALL                                      0  $22     
         44        ASSIGN                                                   !3, $22
  107    45        INIT_FCALL                                               'serialize'
         46        SEND_VAR                                                 !3
         47        DO_ICALL                                         $24     
         48        ASSIGN                                                   !4, $24
  109    49        ECHO                                                     'Lastguest%3A+'
         50        INIT_FCALL                                               'profile'
         51        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FQK8Wu%3A109%243'
         52        BIND_LEXICAL                                             ~26, !0
         53        BIND_LEXICAL                                             ~26, !4
  111    54        SEND_VAL                                                 ~26
         55        DO_FCALL                                      0  $27     
         56        MUL                                              ~28     $27, 1.0e+6
         57        ECHO                                                     ~28
         58        ECHO                                                     '%0A'
         59      > RETURN                                                   1

Function profile:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 10
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 10
Branch analysis from position: 24
Branch analysis from position: 10
filename:       /in/QK8Wu
function name:  profile
number of ops:  26
compiled vars:  !0 = $c, !1 = $n, !2 = $m, !3 = $i, !4 = $t, !5 = $w
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      1000
    3     2        ASSIGN                                                   !2, 'microtime'
    4     3        INIT_FCALL                                               'is_callable'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $7      
          6        MUL                                              ~8      !1, $7
          7        ASSIGN                                                   !3, ~8
          8        ASSIGN                                                   !4, 0
          9      > JMP                                                      ->22
         10    >   INIT_DYNAMIC_CALL                                        !2
         11        SEND_VAL_EX                                              1
         12        DO_FCALL                                      0  $11     
         13        ASSIGN                                                   !5, $11
         14        INIT_DYNAMIC_CALL                                        !0
         15        DO_FCALL                                      0          
         16        INIT_DYNAMIC_CALL                                        !2
         17        SEND_VAL_EX                                              1
         18        DO_FCALL                                      0  $14     
         19        SUB                                              ~15     $14, !5
         20        DIV                                              ~16     ~15, !1
         21        ASSIGN_OP                                     1          !4, ~16
         22    >   POST_DEC                                         ~18     !3
         23      > JMPNZ                                                    ~18, ->10
    5    24    > > RETURN                                                   !4
    6    25*     > RETURN                                                   null

End of function profile

Function dehydrate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QK8Wu
function name:  dehydrate
number of ops:  23
compiled vars:  !0 = $obj, !1 = $class, !2 = $exp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
   50     1        GET_CLASS                                        ~3      !0
          2        ASSIGN                                                   !1, ~3
   51     3        INIT_FCALL                                               'var_export'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 1
          6        DO_ICALL                                         $5      
          7        ASSIGN                                                   !2, $5
   52     8        INIT_FCALL                                               'substr'
          9        SEND_VAR                                                 !2
         10        INIT_FCALL                                               'strpos'
         11        SEND_VAR                                                 !2
         12        SEND_VAL                                                 '%28'
         13        DO_ICALL                                         $7      
         14        ADD                                              ~8      $7, 1
         15        SEND_VAL                                                 ~8
         16        SEND_VAL                                                 -1
         17        DO_ICALL                                         $9      
         18        CONCAT                                           ~10     'return+', $9
         19        CONCAT                                           ~11     ~10, '%3B'
         20        INCLUDE_OR_EVAL                                  $12     ~11, EVAL
         21      > RETURN                                                   $12
   53    22*     > RETURN                                                   null

End of function dehydrate

Function static_hydrate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 21
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 28
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/QK8Wu
function name:  static_hydrate
number of ops:  34
compiled vars:  !0 = $class, !1 = $dry, !2 = $hydrators, !3 = $h
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   56     2        BIND_STATIC                                              !2
   57     3        CONCAT                                           ~4      '_Hydrator__', !0
          4        ASSIGN                                                   !3, ~4
   58     5        ISSET_ISEMPTY_DIM_OBJ                         1          !2, !0
          6      > JMPZ                                                     ~6, ->21
   59     7    >   ROPE_INIT                                    11  ~8      'class+'
          8        ROPE_ADD                                      1  ~8      ~8, !3
          9        ROPE_ADD                                      2  ~8      ~8, '+extends+'
         10        ROPE_ADD                                      3  ~8      ~8, !0
         11        ROPE_ADD                                      4  ~8      ~8, '+%7B%0A++++++++++++++++++static+function+__hydrate%28%24a%29+%7B%0A++++++++++++++++++++++%24o+%3D+new+'
   61    12        ROPE_ADD                                      5  ~8      ~8, !0
         13        ROPE_ADD                                      6  ~8      ~8, '%28%29%3B%0A++++++++++++++++++++++foreach+%28%24a+as+%24n%3D%3E%24v%29+%24o-%3E%24n%3D%24v%3B%0A++++++++++++++++++++++return+%24o%3B%0A++++++++++++++++++%7D+++%0A++++++++++++++%7D+function+hydrate_'
   65    14        ROPE_ADD                                      7  ~8      ~8, !0
         15        ROPE_ADD                                      8  ~8      ~8, '%28%24o%29%7Breturn+'
         16        ROPE_ADD                                      9  ~8      ~8, !3
         17        ROPE_END                                     10  ~7      ~8, '%3A%3A__hydrate%28%24o%29%3B%7D'
         18        INCLUDE_OR_EVAL                                          ~7, EVAL
   67    19        ASSIGN_DIM                                               !2, !0
         20        OP_DATA                                                  <true>
   69    21    > > JMPZ                                                     !1, ->28
         22    >   FETCH_CLASS                                   0  $16     !3
         23        INIT_STATIC_METHOD_CALL                                  $16, '__hydrate'
         24        SEND_VAR_EX                                              !1
         25        DO_FCALL                                      0  $17     
         26        QM_ASSIGN                                        ~18     $17
         27      > JMP                                                      ->32
         28    >   FETCH_CLASS                                   0  $19     !3
         29        NEW                                              $20     $19
         30        DO_FCALL                                      0          
         31        QM_ASSIGN                                        ~18     $20
         32    > > RETURN                                                   ~18
   70    33*     > RETURN                                                   null

End of function static_hydrate

Function hydrate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 21
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 28
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/QK8Wu
function name:  hydrate
number of ops:  34
compiled vars:  !0 = $class, !1 = $dry, !2 = $hydrators, !3 = $h
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   73     2        BIND_STATIC                                              !2
   74     3        CONCAT                                           ~4      '_Hydrator__', !0
          4        ASSIGN                                                   !3, ~4
   75     5        ISSET_ISEMPTY_DIM_OBJ                         1          !2, !0
          6      > JMPZ                                                     ~6, ->21
   76     7    >   ROPE_INIT                                    11  ~8      'class+'
          8        ROPE_ADD                                      1  ~8      ~8, !3
          9        ROPE_ADD                                      2  ~8      ~8, '+extends+'
         10        ROPE_ADD                                      3  ~8      ~8, !0
         11        ROPE_ADD                                      4  ~8      ~8, '+%7B%0A++++++++++++++++++static+function+__hydrate%28%24a%29+%7B%0A++++++++++++++++++++++%24o+%3D+new+'
   78    12        ROPE_ADD                                      5  ~8      ~8, !0
         13        ROPE_ADD                                      6  ~8      ~8, '%28%29%3B%0A++++++++++++++++++++++foreach+%28%24a+as+%24n%3D%3E%24v%29+%24o-%3E%24n%3D%24v%3B%0A++++++++++++++++++++++return+%24o%3B%0A++++++++++++++++++%7D+++%0A++++++++++++++%7D+function+hydrate_'
   82    14        ROPE_ADD                                      7  ~8      ~8, !0
         15        ROPE_ADD                                      8  ~8      ~8, '%28%24o%29%7Breturn+'
         16        ROPE_ADD                                      9  ~8      ~8, !3
         17        ROPE_END                                     10  ~7      ~8, '%3A%3A__hydrate%28%24o%29%3B%7D'
         18        INCLUDE_OR_EVAL                                          ~7, EVAL
   84    19        ASSIGN_DIM                                               !2, !0
         20        OP_DATA                                                  <true>
   86    21    > > JMPZ                                                     !1, ->28
         22    >   FETCH_CLASS                                   0  $16     !3
         23        INIT_STATIC_METHOD_CALL                                  $16, '__hydrate'
         24        SEND_VAR_EX                                              !1
         25        DO_FCALL                                      0  $17     
         26        QM_ASSIGN                                        ~18     $17
         27      > JMP                                                      ->32
         28    >   FETCH_CLASS                                   0  $19     !3
         29        NEW                                              $20     $19
         30        DO_FCALL                                      0          
         31        QM_ASSIGN                                        ~18     $20
         32    > > RETURN                                                   ~18
   87    33*     > RETURN                                                   null

End of function hydrate

Function %00%7Bclosure%7D%2Fin%2FQK8Wu%3A91%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QK8Wu
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $data, !1 = $hydrator, !2 = $hydrated
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
   92     2        NEW                                              $3      'Foo'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !2, $3
   93     5        INIT_METHOD_CALL                                         !1, 'hydrate'
          6        SEND_VAR_EX                                              !0
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0          
   94     9      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FQK8Wu%3A91%240

Function %00%7Bclosure%7D%2Fin%2FQK8Wu%3A97%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QK8Wu
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   97     0  E >   BIND_STATIC                                              !0
   98     1        INIT_FCALL_BY_NAME                                       'hydrate_Foo'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
   99     4      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FQK8Wu%3A97%241

Function %00%7Bclosure%7D%2Fin%2FQK8Wu%3A102%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QK8Wu
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $data, !1 = $hydr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
  103     2        INIT_METHOD_CALL                                         !1, '__hydrate'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
  104     5      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FQK8Wu%3A102%242

Function %00%7Bclosure%7D%2Fin%2FQK8Wu%3A109%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QK8Wu
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $data, !1 = $foo_s
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  109     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
  110     2        INIT_FCALL                                               'unserialize'
          3        SEND_VAR                                                 !1
          4        DO_ICALL                                                 
  111     5      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FQK8Wu%3A109%243

Class Foo: [no user functions]
Class FooSelfHydratingProxy:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QK8Wu
function name:  __construct
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E > > RETURN                                                   null

End of function __construct

Function hydrate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QK8Wu
function name:  hydrate
number of ops:  12
compiled vars:  !0 = $data, !1 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   28     2        FETCH_DIM_R                                      ~3      !0, 'foo'
          3        ASSIGN_OBJ                                               !1, 'foo'
          4        OP_DATA                                                  ~3
   29     5        FETCH_DIM_R                                      ~5      !0, 'bar'
          6        ASSIGN_OBJ                                               !1, 'bar'
          7        OP_DATA                                                  ~5
   30     8        FETCH_DIM_R                                      ~7      !0, 'baz'
          9        ASSIGN_OBJ                                               !1, 'baz'
         10        OP_DATA                                                  ~7
   31    11      > RETURN                                                   null

End of function hydrate

Function extract:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QK8Wu
function name:  extract
number of ops:  9
compiled vars:  !0 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   38     1        FETCH_OBJ_R                                      ~1      !0, 'foo'
          2        INIT_ARRAY                                       ~2      ~1, 'foo'
          3        FETCH_OBJ_R                                      ~3      !0, 'bar'
          4        ADD_ARRAY_ELEMENT                                ~2      ~3, 'bar'
          5        FETCH_OBJ_R                                      ~4      !0, 'baz'
          6        ADD_ARRAY_ELEMENT                                ~2      ~4, 'baz'
          7      > RETURN                                                   ~2
   39     8*     > RETURN                                                   null

End of function extract

End of class FooSelfHydratingProxy.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.77 ms | 1423 KiB | 31 Q