3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Maniple_Model { const CAMELIZE = 0; const UNDERSCORE = 1; /** * @param array $data OPTIONAL * @return void */ public function __construct(array $data = null) // {{{ { if ($data) { $this->setFromArray($data); } } // }}} /** * @param array $data * @return Maniple_Model */ public function setFromArray(array $data) // {{{ { foreach ($data as $key => $value) { $this->__set($key, $value, false); } return $this; } // }}} /** * @return array */ public function toArray($keyTransform = self::CAMELIZE) { $array = array(); foreach (get_object_vars($this) as $property) { // include only properties starting with an underscore, // remove the underscore before retrieving property value if ('_' !== $property[0]) { continue; } $property = substr($property, 1); } return $array; } /** * @param string $key * @param mixed $value * @param bool $throw OPTIONAL * @return Maniple_Model * @throws InvalidArgumentException */ protected function _setProperty($key, $value, $throw = true) // {{{ { $key = self::camelize($key); $setter = 'set' . $key; if (method_exists($this, $setter)) { $this->{$setter}($value); return $this; } $property = '_' . $key; if (property_exists($this, $property)) { $this->{$property} = $value; return $this; } throw new InvalidArgumentException(sprintf('Invalid property: %s', $key)); return false; } // }}} /** * @param string $key * @param bool $throw OPTIONAL * @return mixed * @throws InvalidArgumentException */ protected function _getProperty($key, $throw = true) // {{{ { $key = self::camelize($key); $getter = 'get' . $key; if (method_exists($this, $getter)) { return $this->{$getter}(); } $property = '_' . $key; if (property_exists($this, $property)) { return $this->{$property}; } if ($throw) { throw new InvalidArgumentException(sprintf('Invalid property: %s', $key)); } } // }}} /** * @param string $key * @return bool */ public function has($key) // {{{ { return property_exists($this, '_' . self::camelize($key)); } // }}} /** * @param string $key * @return bool */ public function __isset($key) // {{{ { return isset($this->{'_' . self::camelize($key)}); } // }}} /** * @param string $key * @return mixed * @throws InvalidArgumentException */ public function __get($key) // {{{ { return $this->_getProperty($key, true); } // }}} /** * @param string $key * @param mixed $value * @return void * @throws InvalidArgumentException */ public function __set($key, $value) // {{{ { return $this->_setProperty($key, $value, true); } // }}} /** * Transform given string to camel-case. * * @param string $str * @return string */ public static function camelize($str) // {{{ { if (is_array($str)) { return strtoupper($str[1]); } return preg_replace_callback( '/_(\w)/', array(__CLASS__, __FUNCTION__), (string) $str ); } // }}} /** * Transform given string to underscore separated notation. * * @param string $str * @return string */ public static function underscore($str) { if (is_array($str)) { return $str[1][0] . '_' . strtolower($str[1][1]); } return preg_replace_callback( '/([a-z][A-Z])/', array(__CLASS__, __FUNCTION__), (string) $str ); } /** * @param string $string * @param int $transform * @return string */ public static function transform($string, $transform) // {{{ { switch ($transform) { case self::CAMELIZE: $string = self::camelize($string); break; case self::UNDERSCORE: $string = self::underscore($string); break; } return (string) $string; } // }}} } echo Maniple_Model::camelize('active_record'), "\n"; echo Maniple_Model::camelize('ActiveRecord'), "\n"; echo Maniple_Model::underscore('active_record'), "\n"; echo Maniple_Model::underscore('ActiveRecord'), "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LYU7p
function name:  (null)
number of ops:  21
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  195     0  E >   INIT_STATIC_METHOD_CALL                                  'Maniple_Model', 'camelize'
          1        SEND_VAL                                                 'active_record'
          2        DO_FCALL                                      0  $0      
          3        ECHO                                                     $0
          4        ECHO                                                     '%0A'
  196     5        INIT_STATIC_METHOD_CALL                                  'Maniple_Model', 'camelize'
          6        SEND_VAL                                                 'ActiveRecord'
          7        DO_FCALL                                      0  $1      
          8        ECHO                                                     $1
          9        ECHO                                                     '%0A'
  197    10        INIT_STATIC_METHOD_CALL                                  'Maniple_Model', 'underscore'
         11        SEND_VAL                                                 'active_record'
         12        DO_FCALL                                      0  $2      
         13        ECHO                                                     $2
         14        ECHO                                                     '%0A'
  198    15        INIT_STATIC_METHOD_CALL                                  'Maniple_Model', 'underscore'
         16        SEND_VAL                                                 'ActiveRecord'
         17        DO_FCALL                                      0  $3      
         18        ECHO                                                     $3
         19        ECHO                                                     '%0A'
         20      > RETURN                                                   1

Class Maniple_Model:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 5
Branch analysis from position: 2
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/LYU7p
function name:  __construct
number of ops:  6
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV_INIT                                        !0      null
   14     1      > JMPZ                                                     !0, ->5
   15     2    >   INIT_METHOD_CALL                                         'setFromArray'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
   17     5    > > RETURN                                                   null

End of function __construct

Function setfromarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 10
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/LYU7p
function name:  setFromArray
number of ops:  14
compiled vars:  !0 = $data, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   25     1      > FE_RESET_R                                       $3      !0, ->10
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->10
          3    >   ASSIGN                                                   !2, ~4
   26     4        INIT_METHOD_CALL                                         '__set'
          5        SEND_VAR_EX                                              !2
          6        SEND_VAR_EX                                              !1
          7        SEND_VAL_EX                                              <false>
          8        DO_FCALL                                      0          
   25     9      > JMP                                                      ->2
         10    >   FE_FREE                                                  $3
   28    11        FETCH_THIS                                       ~7      
         12      > RETURN                                                   ~7
   29    13*     > RETURN                                                   null

End of function setfromarray

Function toarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 18
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/LYU7p
function name:  toArray
number of ops:  21
compiled vars:  !0 = $keyTransform, !1 = $array, !2 = $property
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV_INIT                                        !0      <const ast>
   36     1        ASSIGN                                                   !1, <array>
   37     2        INIT_FCALL                                               'get_object_vars'
          3        FETCH_THIS                                       ~4      
          4        SEND_VAL                                                 ~4
          5        DO_ICALL                                         $5      
          6      > FE_RESET_R                                       $6      $5, ->18
          7    > > FE_FETCH_R                                               $6, !2, ->18
   40     8    >   FETCH_DIM_R                                      ~7      !2, 0
          9        IS_NOT_IDENTICAL                                         ~7, '_'
         10      > JMPZ                                                     ~8, ->12
   41    11    > > JMP                                                      ->7
   43    12    >   INIT_FCALL                                               'substr'
         13        SEND_VAR                                                 !2
         14        SEND_VAL                                                 1
         15        DO_ICALL                                         $9      
         16        ASSIGN                                                   !2, $9
   37    17      > JMP                                                      ->7
         18    >   FE_FREE                                                  $6
   46    19      > RETURN                                                   !1
   47    20*     > RETURN                                                   null

End of function toarray

Function _setproperty:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 20
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 32
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/LYU7p
function name:  _setProperty
number of ops:  42
compiled vars:  !0 = $key, !1 = $value, !2 = $throw, !3 = $setter, !4 = $property
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <true>
   58     3        INIT_STATIC_METHOD_CALL                                  'camelize'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !0, $5
   60     7        CONCAT                                           ~7      'set', !0
          8        ASSIGN                                                   !3, ~7
   61     9        INIT_FCALL                                               'method_exists'
         10        FETCH_THIS                                       ~9      
         11        SEND_VAL                                                 ~9
         12        SEND_VAR                                                 !3
         13        DO_ICALL                                         $10     
         14      > JMPZ                                                     $10, ->20
   62    15    >   INIT_METHOD_CALL                                         !3
         16        SEND_VAR_EX                                              !1
         17        DO_FCALL                                      0          
   63    18        FETCH_THIS                                       ~12     
         19      > RETURN                                                   ~12
   66    20    >   CONCAT                                           ~13     '_', !0
         21        ASSIGN                                                   !4, ~13
   67    22        INIT_FCALL                                               'property_exists'
         23        FETCH_THIS                                       ~15     
         24        SEND_VAL                                                 ~15
         25        SEND_VAR                                                 !4
         26        DO_ICALL                                         $16     
         27      > JMPZ                                                     $16, ->32
   68    28    >   ASSIGN_OBJ                                               !4
         29        OP_DATA                                                  !1
   69    30        FETCH_THIS                                       ~18     
         31      > RETURN                                                   ~18
   72    32    >   NEW                                              $19     'InvalidArgumentException'
         33        INIT_FCALL                                               'sprintf'
         34        SEND_VAL                                                 'Invalid+property%3A+%25s'
         35        SEND_VAR                                                 !0
         36        DO_ICALL                                         $20     
         37        SEND_VAR_NO_REF_EX                                       $20
         38        DO_FCALL                                      0          
         39      > THROW                                         0          $19
   74    40*       RETURN                                                   <false>
   75    41*     > RETURN                                                   null

End of function _setproperty

Function _getproperty:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 27
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 36
Branch analysis from position: 28
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LYU7p
function name:  _getProperty
number of ops:  37
compiled vars:  !0 = $key, !1 = $throw, !2 = $getter, !3 = $property
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <true>
   85     2        INIT_STATIC_METHOD_CALL                                  'camelize'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $4      
          5        ASSIGN                                                   !0, $4
   87     6        CONCAT                                           ~6      'get', !0
          7        ASSIGN                                                   !2, ~6
   88     8        INIT_FCALL                                               'method_exists'
          9        FETCH_THIS                                       ~8      
         10        SEND_VAL                                                 ~8
         11        SEND_VAR                                                 !2
         12        DO_ICALL                                         $9      
         13      > JMPZ                                                     $9, ->17
   89    14    >   INIT_METHOD_CALL                                         !2
         15        DO_FCALL                                      0  $10     
         16      > RETURN                                                   $10
   92    17    >   CONCAT                                           ~11     '_', !0
         18        ASSIGN                                                   !3, ~11
   93    19        INIT_FCALL                                               'property_exists'
         20        FETCH_THIS                                       ~13     
         21        SEND_VAL                                                 ~13
         22        SEND_VAR                                                 !3
         23        DO_ICALL                                         $14     
         24      > JMPZ                                                     $14, ->27
   94    25    >   FETCH_OBJ_R                                      ~15     !3
         26      > RETURN                                                   ~15
   97    27    > > JMPZ                                                     !1, ->36
   98    28    >   NEW                                              $16     'InvalidArgumentException'
         29        INIT_FCALL                                               'sprintf'
         30        SEND_VAL                                                 'Invalid+property%3A+%25s'
         31        SEND_VAR                                                 !0
         32        DO_ICALL                                         $17     
         33        SEND_VAR_NO_REF_EX                                       $17
         34        DO_FCALL                                      0          
         35      > THROW                                         0          $16
  100    36    > > RETURN                                                   null

End of function _getproperty

Function has:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LYU7p
function name:  has
number of ops:  12
compiled vars:  !0 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  106     0  E >   RECV                                             !0      
  108     1        INIT_FCALL                                               'property_exists'
          2        FETCH_THIS                                       ~1      
          3        SEND_VAL                                                 ~1
          4        INIT_STATIC_METHOD_CALL                                  'camelize'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $2      
          7        CONCAT                                           ~3      '_', $2
          8        SEND_VAL                                                 ~3
          9        DO_ICALL                                         $4      
         10      > RETURN                                                   $4
  109    11*     > RETURN                                                   null

End of function has

Function __isset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LYU7p
function name:  __isset
number of ops:  8
compiled vars:  !0 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  115     0  E >   RECV                                             !0      
  117     1        INIT_STATIC_METHOD_CALL                                  'camelize'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4        CONCAT                                           ~2      '_', $1
          5        ISSET_ISEMPTY_PROP_OBJ                           ~3      ~2
          6      > RETURN                                                   ~3
  118     7*     > RETURN                                                   null

End of function __isset

Function __get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LYU7p
function name:  __get
number of ops:  7
compiled vars:  !0 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  125     0  E >   RECV                                             !0      
  127     1        INIT_METHOD_CALL                                         '_getProperty'
          2        SEND_VAR_EX                                              !0
          3        SEND_VAL_EX                                              <true>
          4        DO_FCALL                                      0  $1      
          5      > RETURN                                                   $1
  128     6*     > RETURN                                                   null

End of function __get

Function __set:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LYU7p
function name:  __set
number of ops:  9
compiled vars:  !0 = $key, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  136     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  138     2        INIT_METHOD_CALL                                         '_setProperty'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        SEND_VAL_EX                                              <true>
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
  139     8*     > RETURN                                                   null

End of function __set

Function camelize:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LYU7p
function name:  camelize
number of ops:  16
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  147     0  E >   RECV                                             !0      
  149     1        TYPE_CHECK                                  128          !0
          2      > JMPZ                                                     ~1, ->8
  150     3    >   INIT_FCALL                                               'strtoupper'
          4        FETCH_DIM_R                                      ~2      !0, 1
          5        SEND_VAL                                                 ~2
          6        DO_ICALL                                         $3      
          7      > RETURN                                                   $3
  152     8    >   INIT_FCALL                                               'preg_replace_callback'
  153     9        SEND_VAL                                                 '%2F_%28%5Cw%29%2F'
         10        SEND_VAL                                                 <array>
         11        CAST                                          6  ~4      !0
         12        SEND_VAL                                                 ~4
         13        DO_ICALL                                         $5      
         14      > RETURN                                                   $5
  155    15*     > RETURN                                                   null

End of function camelize

Function underscore:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LYU7p
function name:  underscore
number of ops:  21
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  163     0  E >   RECV                                             !0      
  165     1        TYPE_CHECK                                  128          !0
          2      > JMPZ                                                     ~1, ->13
  166     3    >   FETCH_DIM_R                                      ~2      !0, 1
          4        FETCH_DIM_R                                      ~3      ~2, 0
          5        CONCAT                                           ~4      ~3, '_'
          6        INIT_FCALL                                               'strtolower'
          7        FETCH_DIM_R                                      ~5      !0, 1
          8        FETCH_DIM_R                                      ~6      ~5, 1
          9        SEND_VAL                                                 ~6
         10        DO_ICALL                                         $7      
         11        CONCAT                                           ~8      ~4, $7
         12      > RETURN                                                   ~8
  168    13    >   INIT_FCALL                                               'preg_replace_callback'
  169    14        SEND_VAL                                                 '%2F%28%5Ba-z%5D%5BA-Z%5D%29%2F'
         15        SEND_VAL                                                 <array>
         16        CAST                                          6  ~9      !0
         17        SEND_VAL                                                 ~9
         18        DO_ICALL                                         $10     
         19      > RETURN                                                   $10
  171    20*     > RETURN                                                   null

End of function underscore

Function transform:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
filename:       /in/LYU7p
function name:  transform
number of ops:  20
compiled vars:  !0 = $string, !1 = $transform
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  178     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  180     2        IS_EQUAL                                                 !1, 0
          3      > JMPNZ                                                    ~2, ->7
          4    >   IS_EQUAL                                                 !1, 1
          5      > JMPNZ                                                    ~2, ->12
          6    > > JMP                                                      ->17
  182     7    >   INIT_STATIC_METHOD_CALL                                  'camelize'
          8        SEND_VAR                                                 !0
          9        DO_FCALL                                      0  $3      
         10        ASSIGN                                                   !0, $3
  183    11      > JMP                                                      ->17
  186    12    >   INIT_STATIC_METHOD_CALL                                  'underscore'
         13        SEND_VAR                                                 !0
         14        DO_FCALL                                      0  $5      
         15        ASSIGN                                                   !0, $5
  187    16      > JMP                                                      ->17
  190    17    >   CAST                                          6  ~7      !0
         18      > RETURN                                                   ~7
  191    19*     > RETURN                                                   null

End of function transform

End of class Maniple_Model.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.42 ms | 1420 KiB | 29 Q