3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @author Quyen Bui <buiquyen1995@gmail.com> */ trait LazyGetSet { private $useFluentSetter = true; /** * Change fluent setter. */ public function setFluentSetter($flag) { $this->useFluentSetter = $flag; } public function __call($method, $args) { if ((0 === strpos($method, 'get')) && ($ptyName = $this->getLazyPropertyName($method))) { return $this->lazyGet($ptyName, $args); } if ((0 === strpos($method, 'set')) && ($ptyName = $this->getLazyPropertyName($method))) { return $this->lazySet($ptyName, $args); } } /** * Detect property name from setter/getter methods. */ private function getLazyPropertyName($method) { $ptyName = lcfirst(substr($method, 3)); if (property_exists($this, $ptyName)) { return $ptyName; } } /** * Get property value */ private function lazyGet($ptyName) { return $this->{$ptyName}; } /** * Inject property value. * * @param string $ptyName * @param mixed[] $args * @return null|self */ private function lazySet($ptyName, $args) { $this->{$ptyName} = $args[0]; if ($this->useFluentSetter) { return $this; } } } // --------------------- /// Demo // --------------------- class MyClass { use LazyGetSet; private $name; } echo (new MyClass())->setName(123)->getName();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rPmZD
function name:  (null)
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   DECLARE_CLASS                                            'myclass'
   79     1        NEW                                              $0      'MyClass'
          2        DO_FCALL                                      0          
          3        INIT_METHOD_CALL                                         $0, 'setName'
          4        SEND_VAL_EX                                              123
          5        DO_FCALL                                      0  $2      
          6        INIT_METHOD_CALL                                         $2, 'getName'
          7        DO_FCALL                                      0  $3      
          8        ECHO                                                     $3
          9      > RETURN                                                   1

Class LazyGetSet:
Function setfluentsetter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rPmZD
function name:  setFluentSetter
number of ops:  4
compiled vars:  !0 = $flag
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   16     1        ASSIGN_OBJ                                               'useFluentSetter'
          2        OP_DATA                                                  !0
   17     3      > RETURN                                                   null

End of function setfluentsetter

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 19
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 46) Position 1 = 25, Position 2 = 30
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 36
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
Branch analysis from position: 13
filename:       /in/rPmZD
function name:  __call
number of ops:  37
compiled vars:  !0 = $method, !1 = $args, !2 = $ptyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   21     2        INIT_FCALL                                               'strpos'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 'get'
          5        DO_ICALL                                         $3      
          6        IS_IDENTICAL                                     ~4      $3, 0
          7      > JMPZ_EX                                          ~4      ~4, ->13
          8    >   INIT_METHOD_CALL                                         'getLazyPropertyName'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0  $5      
         11        ASSIGN                                           ~6      !2, $5
         12        BOOL                                             ~4      ~6
         13    > > JMPZ                                                     ~4, ->19
   22    14    >   INIT_METHOD_CALL                                         'lazyGet'
         15        SEND_VAR_EX                                              !2
         16        SEND_VAR_EX                                              !1
         17        DO_FCALL                                      0  $7      
         18      > RETURN                                                   $7
   25    19    >   INIT_FCALL                                               'strpos'
         20        SEND_VAR                                                 !0
         21        SEND_VAL                                                 'set'
         22        DO_ICALL                                         $8      
         23        IS_IDENTICAL                                     ~9      $8, 0
         24      > JMPZ_EX                                          ~9      ~9, ->30
         25    >   INIT_METHOD_CALL                                         'getLazyPropertyName'
         26        SEND_VAR_EX                                              !0
         27        DO_FCALL                                      0  $10     
         28        ASSIGN                                           ~11     !2, $10
         29        BOOL                                             ~9      ~11
         30    > > JMPZ                                                     ~9, ->36
   26    31    >   INIT_METHOD_CALL                                         'lazySet'
         32        SEND_VAR_EX                                              !2
         33        SEND_VAR_EX                                              !1
         34        DO_FCALL                                      0  $12     
         35      > RETURN                                                   $12
   28    36    > > RETURN                                                   null

End of function __call

Function getlazypropertyname:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rPmZD
function name:  getLazyPropertyName
number of ops:  17
compiled vars:  !0 = $method, !1 = $ptyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   35     1        INIT_FCALL                                               'lcfirst'
          2        INIT_FCALL                                               'substr'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 3
          5        DO_ICALL                                         $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                         $3      
          8        ASSIGN                                                   !1, $3
   36     9        INIT_FCALL                                               'property_exists'
         10        FETCH_THIS                                       ~5      
         11        SEND_VAL                                                 ~5
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $6      
         14      > JMPZ                                                     $6, ->16
   37    15    > > RETURN                                                   !1
   39    16    > > RETURN                                                   null

End of function getlazypropertyname

Function lazyget:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rPmZD
function name:  lazyGet
number of ops:  4
compiled vars:  !0 = $ptyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        FETCH_OBJ_R                                      ~1      !0
          2      > RETURN                                                   ~1
   47     3*     > RETURN                                                   null

End of function lazyget

Function lazyset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rPmZD
function name:  lazySet
number of ops:  10
compiled vars:  !0 = $ptyName, !1 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   58     2        FETCH_DIM_R                                      ~3      !1, 0
          3        ASSIGN_OBJ                                               !0
          4        OP_DATA                                                  ~3
   60     5        FETCH_OBJ_R                                      ~4      'useFluentSetter'
          6      > JMPZ                                                     ~4, ->9
   61     7    >   FETCH_THIS                                       ~5      
          8      > RETURN                                                   ~5
   63     9    > > RETURN                                                   null

End of function lazyset

End of class LazyGetSet.

Class MyClass: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.55 ms | 1449 KiB | 17 Q