3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Item { private $cost = 100; public function getTotal($discount = 7) { return self::cost * $discount/(100+$discount); } } class AccesserToPrivated { public function getPrivate($obj, $attribute) { $getter = function() use ($attribute) {return $this->$attribute;}; return \Closure::bind($getter, $obj, get_class($obj)); } public function setPrivate($obj, $attribute) { $setter = function($value) use ($attribute) {$this->$attribute = $value;}; return \Closure::bind($setter, $obj, get_class($obj)); } } $obj = new Item(); $accesser = new AccesserToPrivated(); $getFoo = $accesser->getPrivate($obj, 'cost'); $setFoo = $accesser->setPrivate($obj, 'cost'); echo $obj->getTotal() . PHP_EOL; echo $obj->getFoo(); $setFoo(1); echo $obj->getTotal() . PHP_EOL; echo $obj->getFoo();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KvuYJ
function name:  (null)
number of ops:  34
compiled vars:  !0 = $obj, !1 = $accesser, !2 = $getFoo, !3 = $setFoo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   NEW                                              $4      'Item'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $4
   26     3        NEW                                              $7      'AccesserToPrivated'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $7
   27     6        INIT_METHOD_CALL                                         !1, 'getPrivate'
          7        SEND_VAR_EX                                              !0
          8        SEND_VAL_EX                                              'cost'
          9        DO_FCALL                                      0  $10     
         10        ASSIGN                                                   !2, $10
   28    11        INIT_METHOD_CALL                                         !1, 'setPrivate'
         12        SEND_VAR_EX                                              !0
         13        SEND_VAL_EX                                              'cost'
         14        DO_FCALL                                      0  $12     
         15        ASSIGN                                                   !3, $12
   30    16        INIT_METHOD_CALL                                         !0, 'getTotal'
         17        DO_FCALL                                      0  $14     
         18        CONCAT                                           ~15     $14, '%0A'
         19        ECHO                                                     ~15
   31    20        INIT_METHOD_CALL                                         !0, 'getFoo'
         21        DO_FCALL                                      0  $16     
         22        ECHO                                                     $16
   32    23        INIT_DYNAMIC_CALL                                        !3
         24        SEND_VAL_EX                                              1
         25        DO_FCALL                                      0          
   33    26        INIT_METHOD_CALL                                         !0, 'getTotal'
         27        DO_FCALL                                      0  $18     
         28        CONCAT                                           ~19     $18, '%0A'
         29        ECHO                                                     ~19
   34    30        INIT_METHOD_CALL                                         !0, 'getFoo'
         31        DO_FCALL                                      0  $20     
         32        ECHO                                                     $20
         33      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FKvuYJ%3A14%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KvuYJ
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $attribute
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   BIND_STATIC                                              !0
          1        FETCH_THIS                                       $1      
          2        FETCH_OBJ_R                                      ~2      $1, !0
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FKvuYJ%3A14%240

Function %00%7Bclosure%7D%2Fin%2FKvuYJ%3A19%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KvuYJ
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $value, !1 = $attribute
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        FETCH_THIS                                       $2      
          3        ASSIGN_OBJ                                               $2, !1
          4        OP_DATA                                                  !0
          5      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FKvuYJ%3A19%241

Class Item:
Function gettotal:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KvuYJ
function name:  getTotal
number of ops:  7
compiled vars:  !0 = $discount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV_INIT                                        !0      7
    7     1        FETCH_CLASS_CONSTANT                             ~1      'cost'
          2        MUL                                              ~2      !0, ~1
          3        ADD                                              ~3      100, !0
          4        DIV                                              ~4      ~2, ~3
          5      > RETURN                                                   ~4
    8     6*     > RETURN                                                   null

End of function gettotal

End of class Item.

Class AccesserToPrivated:
Function getprivate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KvuYJ
function name:  getPrivate
number of ops:  13
compiled vars:  !0 = $obj, !1 = $attribute, !2 = $getter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FKvuYJ%3A14%240'
          3        BIND_LEXICAL                                             ~3, !1
          4        ASSIGN                                                   !2, ~3
   15     5        INIT_STATIC_METHOD_CALL                                  'Closure', 'bind'
          6        SEND_VAR                                                 !2
          7        SEND_VAR                                                 !0
          8        GET_CLASS                                        ~5      !0
          9        SEND_VAL                                                 ~5
         10        DO_FCALL                                      0  $6      
         11      > RETURN                                                   $6
   16    12*     > RETURN                                                   null

End of function getprivate

Function setprivate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KvuYJ
function name:  setPrivate
number of ops:  13
compiled vars:  !0 = $obj, !1 = $attribute, !2 = $setter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   19     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FKvuYJ%3A19%241'
          3        BIND_LEXICAL                                             ~3, !1
          4        ASSIGN                                                   !2, ~3
   20     5        INIT_STATIC_METHOD_CALL                                  'Closure', 'bind'
          6        SEND_VAR                                                 !2
          7        SEND_VAR                                                 !0
          8        GET_CLASS                                        ~5      !0
          9        SEND_VAL                                                 ~5
         10        DO_FCALL                                      0  $6      
         11      > RETURN                                                   $6
   21    12*     > RETURN                                                   null

End of function setprivate

End of class AccesserToPrivated.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.54 ms | 1403 KiB | 13 Q