3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Str { /** PHPの組み込み関数の第一引数に使う値をプロパティとしてセット */ public function __construct( public string $str ){} /** * このクラスで外部に公開していない名前のメソッドが呼ばれた場合に動作するマジックメソッド * @see https://www.php.net/manual/ja/language.oop5.overloading.php#object.call */ public function __call($name, $arguments) { // 呼ばれたメソッド名の関数を第一引数を最初に与えたプロパティ、 // 第二引数以降を呼ばれた際の引数として実行 $result = $name($this->str, ...$arguments); // 関数の返り値が文字列ならば返り値を元に自身のインスタンスを生成して返却 // 関数の返り値が文字列でないならば返り値をそのまま返却 return is_string($result) ? new self($result) : $result; } /** __call に処理を渡さず、第二引数にプロパティの値を渡して explode 関数を実行するメソッドを用意する */ public function explode(string $separator, ?int $limit = PHP_INT_MAX) { return explode($separator, $this->str, $limit); } } $s = new Str('hoge'); $res = $s->substr(0, 2) ->strtoupper() ->str_pad(4, '0', STR_PAD_LEFT) ->explode('H'); var_dump($res); /* array(2) { [0]=> string(2) "00" [1]=> string(1) "O" } */
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/17rYQ
function name:  (null)
number of ops:  23
compiled vars:  !0 = $s, !1 = $res
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   NEW                                              $2      'Str'
          1        SEND_VAL_EX                                              'hoge'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
   29     4        INIT_METHOD_CALL                                         !0, 'substr'
          5        SEND_VAL_EX                                              0
          6        SEND_VAL_EX                                              2
          7        DO_FCALL                                      0  $5      
   30     8        INIT_METHOD_CALL                                         $5, 'strtoupper'
          9        DO_FCALL                                      0  $6      
   31    10        INIT_METHOD_CALL                                         $6, 'str_pad'
         11        SEND_VAL_EX                                              4
         12        SEND_VAL_EX                                              '0'
         13        SEND_VAL_EX                                              0
         14        DO_FCALL                                      0  $7      
   32    15        INIT_METHOD_CALL                                         $7, 'explode'
         16        SEND_VAL_EX                                              'H'
         17        DO_FCALL                                      0  $8      
   29    18        ASSIGN                                                   !1, $8
   33    19        INIT_FCALL                                               'var_dump'
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                                 
   41    22      > RETURN                                                   1

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

End of function __construct

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 17
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/17rYQ
function name:  __call
number of ops:  20
compiled vars:  !0 = $name, !1 = $arguments, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   16     2        INIT_DYNAMIC_CALL                                        !0
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $3      'str'
          5        SEND_FUNC_ARG                                            $3
          6        SEND_UNPACK                                              !1
          7        CHECK_UNDEF_ARGS                                         
          8        DO_FCALL                                      1  $4      
          9        ASSIGN                                                   !2, $4
   19    10        TYPE_CHECK                                   64          !2
         11      > JMPZ                                                     ~6, ->17
         12    >   NEW                          self                $7      
         13        SEND_VAR_EX                                              !2
         14        DO_FCALL                                      0          
         15        QM_ASSIGN                                        ~9      $7
         16      > JMP                                                      ->18
         17    >   QM_ASSIGN                                        ~9      !2
         18    > > RETURN                                                   ~9
   20    19*     > RETURN                                                   null

End of function __call

Function explode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/17rYQ
function name:  explode
number of ops:  10
compiled vars:  !0 = $separator, !1 = $limit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <const ast>
   25     2        INIT_FCALL                                               'explode'
          3        SEND_VAR                                                 !0
          4        FETCH_OBJ_R                                      ~2      'str'
          5        SEND_VAL                                                 ~2
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $3      
          8      > RETURN                                                   $3
   26     9*     > RETURN                                                   null

End of function explode

End of class Str.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.01 ms | 1451 KiB | 15 Q