3v4l.org

run code in 300+ PHP versions simultaneously
<?php // note that copy() is a proper alternative to __clone() interface Any { function getHash(); function equals($other); } class Mutable implements Any { protected $baz; function __construct(string $baz){ $this->setBaz($baz); } function setBaz(string $baz){ $this->baz = $baz; } function getBaz(){ return $this->baz; } function getHash(){ return Immutable::CLASS . "|" . spl_object_hash($this); } function equals($other){ return $this->getHash() === $other->getHash(); } function copy(){ return new static($this->baz); } } class Immutable implements Any { protected $baz; function __construct(string $baz){ $this->baz = $baz; } function getHash(){ return Mutable::CLASS . "|" . $this->baz; } function equals($other){ return $this->getHash() === $other->getHash(); } function copy(){ return $this; } } var_dump((new Immutable("baz"))->equals(new Immutable("baz"))); // true var_dump((new Immutable("bar"))->equals(new Immutable("bar"))); // true var_dump((new Mutable("baz"))->equals(new Mutable("baz"))); // false var_dump((new Mutable("bar"))->equals(new Mutable("bar"))); // false
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ha14g
function name:  (null)
number of ops:  51
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   DECLARE_CLASS                                            'mutable'
   40     1        DECLARE_CLASS                                            'immutable'
   61     2        INIT_FCALL                                               'var_dump'
          3        NEW                                              $0      'Immutable'
          4        SEND_VAL_EX                                              'baz'
          5        DO_FCALL                                      0          
          6        INIT_METHOD_CALL                                         $0, 'equals'
          7        NEW                                              $2      'Immutable'
          8        SEND_VAL_EX                                              'baz'
          9        DO_FCALL                                      0          
         10        SEND_VAR_NO_REF_EX                                       $2
         11        DO_FCALL                                      0  $4      
         12        SEND_VAR                                                 $4
         13        DO_ICALL                                                 
   62    14        INIT_FCALL                                               'var_dump'
         15        NEW                                              $6      'Immutable'
         16        SEND_VAL_EX                                              'bar'
         17        DO_FCALL                                      0          
         18        INIT_METHOD_CALL                                         $6, 'equals'
         19        NEW                                              $8      'Immutable'
         20        SEND_VAL_EX                                              'bar'
         21        DO_FCALL                                      0          
         22        SEND_VAR_NO_REF_EX                                       $8
         23        DO_FCALL                                      0  $10     
         24        SEND_VAR                                                 $10
         25        DO_ICALL                                                 
   64    26        INIT_FCALL                                               'var_dump'
         27        NEW                                              $12     'Mutable'
         28        SEND_VAL_EX                                              'baz'
         29        DO_FCALL                                      0          
         30        INIT_METHOD_CALL                                         $12, 'equals'
         31        NEW                                              $14     'Mutable'
         32        SEND_VAL_EX                                              'baz'
         33        DO_FCALL                                      0          
         34        SEND_VAR_NO_REF_EX                                       $14
         35        DO_FCALL                                      0  $16     
         36        SEND_VAR                                                 $16
         37        DO_ICALL                                                 
   65    38        INIT_FCALL                                               'var_dump'
         39        NEW                                              $18     'Mutable'
         40        SEND_VAL_EX                                              'bar'
         41        DO_FCALL                                      0          
         42        INIT_METHOD_CALL                                         $18, 'equals'
         43        NEW                                              $20     'Mutable'
         44        SEND_VAL_EX                                              'bar'
         45        DO_FCALL                                      0          
         46        SEND_VAR_NO_REF_EX                                       $20
         47        DO_FCALL                                      0  $22     
         48        SEND_VAR                                                 $22
         49        DO_ICALL                                                 
         50      > RETURN                                                   1

Class Any:
Function gethash:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ha14g
function name:  getHash
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E > > RETURN                                                   null

End of function gethash

Function equals:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ha14g
function name:  equals
number of ops:  2
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function equals

End of class Any.

Class Mutable:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ha14g
function name:  __construct
number of ops:  5
compiled vars:  !0 = $baz
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   16     1        INIT_METHOD_CALL                                         'setBaz'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
   17     4      > RETURN                                                   null

End of function __construct

Function setbaz:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ha14g
function name:  setBaz
number of ops:  4
compiled vars:  !0 = $baz
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   20     1        ASSIGN_OBJ                                               'baz'
          2        OP_DATA                                                  !0
   21     3      > RETURN                                                   null

End of function setbaz

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

End of function getbaz

Function gethash:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ha14g
function name:  getHash
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   INIT_FCALL                                               'spl_object_hash'
          1        FETCH_THIS                                       ~0      
          2        SEND_VAL                                                 ~0
          3        DO_ICALL                                         $1      
          4        CONCAT                                           ~2      'Immutable%7C', $1
          5      > RETURN                                                   ~2
   29     6*     > RETURN                                                   null

End of function gethash

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

End of function equals

Function copy:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ha14g
function name:  copy
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   NEW                          static              $0      
          1        CHECK_FUNC_ARG                                           
          2        FETCH_OBJ_FUNC_ARG                               $1      'baz'
          3        SEND_FUNC_ARG                                            $1
          4        DO_FCALL                                      0          
          5      > RETURN                                                   $0
   37     6*     > RETURN                                                   null

End of function copy

End of class Mutable.

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

End of function __construct

Function gethash:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ha14g
function name:  getHash
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   FETCH_OBJ_R                                      ~0      'baz'
          1        CONCAT                                           ~1      'Mutable%7C', ~0
          2      > RETURN                                                   ~1
   50     3*     > RETURN                                                   null

End of function gethash

Function equals:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ha14g
function name:  equals
number of ops:  8
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   53     1        INIT_METHOD_CALL                                         'getHash'
          2        DO_FCALL                                      0  $1      
          3        INIT_METHOD_CALL                                         !0, 'getHash'
          4        DO_FCALL                                      0  $2      
          5        IS_IDENTICAL                                     ~3      $1, $2
          6      > RETURN                                                   ~3
   54     7*     > RETURN                                                   null

End of function equals

Function copy:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ha14g
function name:  copy
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   FETCH_THIS                                       ~0      
          1      > RETURN                                                   ~0
   58     2*     > RETURN                                                   null

End of function copy

End of class Immutable.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.89 ms | 1408 KiB | 17 Q