3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* A very simple implementation of 'typed array', like this: $stringArray = new TypedArray('string'); and now only strings can be added to the above $stringArray. Works with objects as well. More examples below. */ class TypedArray extends \ArrayIterator { protected $type = ''; public function __construct(string $type, array $input = [], int $flags = 0){ $this->type = $type; parent::__construct($input, $flags); } /* Just an utility funciton. In case it's a scalar (like int), or array etc - returns that if it's an object, returns the name of the class. */ public function getTypeOrClassName($val){ $testType = gettype($val); if($testType === 'object'){ return get_class($val); } return $testType; } public function checkType($val){ return $this->getTypeOrClassName($val) === $this->type; } public function getType(){ return $this->type; } public function offsetSet($offset, $value) { if(!$this->checkType($value)){ throw new \InvalidArgumentException('This TypedArray accepts only "' . $this->type . '" type, "' . $this->getTypeOrClassName($value) . '" given'); } return parent::offsetSet($offset, $value); } } ////////////////////////////////////////////////////////////////////////////// //examples $stringArray = new TypedArray('string'); $stringArray []= 'first string'; $stringArray []= 'second string'; //$stringArray [] = 2;//won't work - exception thrown var_dump((array)$stringArray); //tests with objects as values class User{ public $name; public function __construct($name){ $this->name = $name; } } $userArray = new TypedArray(User::class); $userArray[]= new User('John'); //$userArray []= new \StdClass();//exception thrown echo $userArray[0]->name;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kHocv
function name:  (null)
number of ops:  25
compiled vars:  !0 = $stringArray, !1 = $userArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   NEW                                              $2      'TypedArray'
          1        SEND_VAL_EX                                              'string'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
   52     4        ASSIGN_DIM                                               !0
          5        OP_DATA                                                  'first+string'
   53     6        ASSIGN_DIM                                               !0
          7        OP_DATA                                                  'second+string'
   57     8        INIT_FCALL                                               'var_dump'
          9        CAST                                          7  ~7      !0
         10        SEND_VAL                                                 ~7
         11        DO_ICALL                                                 
   69    12        NEW                                              $9      'TypedArray'
         13        SEND_VAL_EX                                              'User'
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !1, $9
   70    16        NEW                                              $13     'User'
         17        SEND_VAL_EX                                              'John'
         18        DO_FCALL                                      0          
         19        ASSIGN_DIM                                               !1
         20        OP_DATA                                                  $13
   74    21        FETCH_DIM_R                                      ~15     !1, 0
         22        FETCH_OBJ_R                                      ~16     ~15, 'name'
         23        ECHO                                                     ~16
   75    24      > RETURN                                                   1

Class TypedArray:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kHocv
function name:  __construct
number of ops:  10
compiled vars:  !0 = $type, !1 = $input, !2 = $flags
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      0
   15     3        ASSIGN_OBJ                                               'type'
          4        OP_DATA                                                  !0
   16     5        INIT_STATIC_METHOD_CALL                                  
          6        SEND_VAR_EX                                              !1
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0          
   17     9      > RETURN                                                   null

End of function __construct

Function gettypeorclassname:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kHocv
function name:  getTypeOrClassName
number of ops:  9
compiled vars:  !0 = $val, !1 = $testType
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   24     1        GET_TYPE                                         ~2      !0
          2        ASSIGN                                                   !1, ~2
   25     3        IS_IDENTICAL                                             !1, 'object'
          4      > JMPZ                                                     ~4, ->7
   26     5    >   GET_CLASS                                        ~5      !0
          6      > RETURN                                                   ~5
   28     7    > > RETURN                                                   !1
   29     8*     > RETURN                                                   null

End of function gettypeorclassname

Function checktype:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kHocv
function name:  checkType
number of ops:  8
compiled vars:  !0 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   32     1        INIT_METHOD_CALL                                         'getTypeOrClassName'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4        FETCH_OBJ_R                                      ~2      'type'
          5        IS_IDENTICAL                                     ~3      $1, ~2
          6      > RETURN                                                   ~3
   33     7*     > RETURN                                                   null

End of function checktype

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

End of function gettype

Function offsetset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 19
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kHocv
function name:  offsetSet
number of ops:  25
compiled vars:  !0 = $offset, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   41     2        INIT_METHOD_CALL                                         'checkType'
          3        SEND_VAR_EX                                              !1
          4        DO_FCALL                                      0  $2      
          5        BOOL_NOT                                         ~3      $2
          6      > JMPZ                                                     ~3, ->19
   42     7    >   NEW                                              $4      'InvalidArgumentException'
          8        FETCH_OBJ_R                                      ~5      'type'
          9        CONCAT                                           ~6      'This+TypedArray+accepts+only+%22', ~5
         10        CONCAT                                           ~7      ~6, '%22+type%2C+%22'
         11        INIT_METHOD_CALL                                         'getTypeOrClassName'
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0  $8      
         14        CONCAT                                           ~9      ~7, $8
         15        CONCAT                                           ~10     ~9, '%22+given'
         16        SEND_VAL_EX                                              ~10
         17        DO_FCALL                                      0          
         18      > THROW                                         0          $4
   44    19    >   INIT_STATIC_METHOD_CALL                                  'offsetSet'
         20        SEND_VAR_EX                                              !0
         21        SEND_VAR_EX                                              !1
         22        DO_FCALL                                      0  $12     
         23      > RETURN                                                   $12
   45    24*     > RETURN                                                   null

End of function offsetset

End of class TypedArray.

Class User:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kHocv
function name:  __construct
number of ops:  4
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
   65     1        ASSIGN_OBJ                                               'name'
          2        OP_DATA                                                  !0
   66     3      > RETURN                                                   null

End of function __construct

End of class User.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146 ms | 1014 KiB | 14 Q