3v4l.org

run code in 300+ PHP versions simultaneously
<?php function hashcode($str){ // return sha1($str); return $str; } class SomethingBuilder { private $name; private $super; function __construct($name){ $this->name = $name; } function getName(){ return $this->name; } function setSuper(SomethingBuilder $super = null){ $this->super = $super; } function getSuper(){ return $this->super; } function getHash(){ return hashcode(spl_object_hash($this)); } function equals($other){ return $this->getHash() === $other->getHash(); } } class Something { protected $super; protected $name; function __construct(SomethingBuilder $builder){ $this->name = $builder->getName(); $argBuilder = $builder; $immutables = []; $builders = []; do{ if(isset($immutables[$builder->getHash()])) break; // this should rather be a private class, which php doesn't have yet, but better than nothing. $immutable = $builder === $argBuilder ? $this : new class($builder->getName()) extends Something{ function __construct($name){ $this->name = $name; } }; $immutables[$builder->getHash()] = $immutable; $builders[$builder->getHash()] = $builder; }while($builder = $builder->getSuper()); foreach($immutables as $builderHash => $immutable){ $immutable->super = $immutables[$builders[$builderHash]->getSuper()->getHash()]; } } function getName(){ return $this->name; } function getSuper(){ return $this->super; } function getHash($origin = null){ if($this === $origin){ return hashcode("*RECURSION*"); }else{ return hashcode( "Something" . "{" . $this->getName() . "|" . $this->getSuper()->getHash($origin ?? $this) . "}" ); } } function equals($other){ return $this->getHash() === $other->getHash(); } } $a = new SomethingBuilder("A"); $b = new SomethingBuilder("B"); $c = new SomethingBuilder("C"); $a->setSuper($c); $b->setSuper($a); $c->setSuper($b); $immutable = new Something($a); var_dump($immutable->getHash()); var_dump($immutable->getSuper()->getHash()); var_dump($immutable->getSuper()->getSuper()->getHash()); var_dump($immutable->getSuper()->getSuper()->getSuper()->getHash()); var_dump($immutable->getSuper()->getSuper()->getSuper()->getSuper()->getHash()); assert($immutable === $immutable->getSuper()->getSuper()->getSuper()); assert( $immutable->equals( $immutable->getSuper()->getSuper()->getSuper() ) ); echo "\n\n"; $a = new SomethingBuilder("A"); $a->setSuper($a); $immutable = new Something($a); var_dump($immutable->getHash()); var_dump($immutable->getSuper()->getHash()); assert($immutable === $immutable->getSuper()); assert($immutable->equals($immutable->getSuper()));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HVcam
function name:  (null)
number of ops:  139
compiled vars:  !0 = $a, !1 = $b, !2 = $c, !3 = $immutable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   94     0  E >   NEW                                              $4      'SomethingBuilder'
          1        SEND_VAL_EX                                              'A'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $4
   95     4        NEW                                              $7      'SomethingBuilder'
          5        SEND_VAL_EX                                              'B'
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $7
   96     8        NEW                                              $10     'SomethingBuilder'
          9        SEND_VAL_EX                                              'C'
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !2, $10
   97    12        INIT_METHOD_CALL                                         !0, 'setSuper'
         13        SEND_VAR_EX                                              !2
         14        DO_FCALL                                      0          
   98    15        INIT_METHOD_CALL                                         !1, 'setSuper'
         16        SEND_VAR_EX                                              !0
         17        DO_FCALL                                      0          
   99    18        INIT_METHOD_CALL                                         !2, 'setSuper'
         19        SEND_VAR_EX                                              !1
         20        DO_FCALL                                      0          
  100    21        NEW                                              $16     'Something'
         22        SEND_VAR_EX                                              !0
         23        DO_FCALL                                      0          
         24        ASSIGN                                                   !3, $16
  101    25        INIT_FCALL                                               'var_dump'
         26        INIT_METHOD_CALL                                         !3, 'getHash'
         27        DO_FCALL                                      0  $19     
         28        SEND_VAR                                                 $19
         29        DO_ICALL                                                 
  102    30        INIT_FCALL                                               'var_dump'
         31        INIT_METHOD_CALL                                         !3, 'getSuper'
         32        DO_FCALL                                      0  $21     
         33        INIT_METHOD_CALL                                         $21, 'getHash'
         34        DO_FCALL                                      0  $22     
         35        SEND_VAR                                                 $22
         36        DO_ICALL                                                 
  103    37        INIT_FCALL                                               'var_dump'
         38        INIT_METHOD_CALL                                         !3, 'getSuper'
         39        DO_FCALL                                      0  $24     
         40        INIT_METHOD_CALL                                         $24, 'getSuper'
         41        DO_FCALL                                      0  $25     
         42        INIT_METHOD_CALL                                         $25, 'getHash'
         43        DO_FCALL                                      0  $26     
         44        SEND_VAR                                                 $26
         45        DO_ICALL                                                 
  104    46        INIT_FCALL                                               'var_dump'
         47        INIT_METHOD_CALL                                         !3, 'getSuper'
         48        DO_FCALL                                      0  $28     
         49        INIT_METHOD_CALL                                         $28, 'getSuper'
         50        DO_FCALL                                      0  $29     
         51        INIT_METHOD_CALL                                         $29, 'getSuper'
         52        DO_FCALL                                      0  $30     
         53        INIT_METHOD_CALL                                         $30, 'getHash'
         54        DO_FCALL                                      0  $31     
         55        SEND_VAR                                                 $31
         56        DO_ICALL                                                 
  105    57        INIT_FCALL                                               'var_dump'
         58        INIT_METHOD_CALL                                         !3, 'getSuper'
         59        DO_FCALL                                      0  $33     
         60        INIT_METHOD_CALL                                         $33, 'getSuper'
         61        DO_FCALL                                      0  $34     
         62        INIT_METHOD_CALL                                         $34, 'getSuper'
         63        DO_FCALL                                      0  $35     
         64        INIT_METHOD_CALL                                         $35, 'getSuper'
         65        DO_FCALL                                      0  $36     
         66        INIT_METHOD_CALL                                         $36, 'getHash'
         67        DO_FCALL                                      0  $37     
         68        SEND_VAR                                                 $37
         69        DO_ICALL                                                 
  106    70        ASSERT_CHECK                                             
         71        INIT_FCALL                                               'assert'
         72        INIT_METHOD_CALL                                         !3, 'getSuper'
         73        DO_FCALL                                      0  $39     
         74        INIT_METHOD_CALL                                         $39, 'getSuper'
         75        DO_FCALL                                      0  $40     
         76        INIT_METHOD_CALL                                         $40, 'getSuper'
         77        DO_FCALL                                      0  $41     
         78        IS_IDENTICAL                                     ~42     !3, $41
         79        SEND_VAL                                                 ~42
         80        SEND_VAL                                                 'assert%28%24immutable+%3D%3D%3D+%24immutable-%3EgetSuper%28%29-%3EgetSuper%28%29-%3EgetSuper%28%29%29'
         81        DO_ICALL                                                 
  107    82        ASSERT_CHECK                                             
         83        INIT_FCALL                                               'assert'
  108    84        INIT_METHOD_CALL                                         !3, 'equals'
  109    85        INIT_METHOD_CALL                                         !3, 'getSuper'
         86        DO_FCALL                                      0  $44     
         87        INIT_METHOD_CALL                                         $44, 'getSuper'
         88        DO_FCALL                                      0  $45     
         89        INIT_METHOD_CALL                                         $45, 'getSuper'
         90        DO_FCALL                                      0  $46     
         91        SEND_VAR_NO_REF_EX                                       $46
         92        DO_FCALL                                      0  $47     
         93        SEND_VAR                                                 $47
  107    94        SEND_VAL                                                 'assert%28%24immutable-%3Eequals%28%24immutable-%3EgetSuper%28%29-%3EgetSuper%28%29-%3EgetSuper%28%29%29%29'
         95        DO_ICALL                                                 
  113    96        ECHO                                                     '%0A%0A'
  115    97        NEW                                              $49     'SomethingBuilder'
         98        SEND_VAL_EX                                              'A'
         99        DO_FCALL                                      0          
        100        ASSIGN                                                   !0, $49
  116   101        INIT_METHOD_CALL                                         !0, 'setSuper'
        102        SEND_VAR_EX                                              !0
        103        DO_FCALL                                      0          
  117   104        NEW                                              $53     'Something'
        105        SEND_VAR_EX                                              !0
        106        DO_FCALL                                      0          
        107        ASSIGN                                                   !3, $53
  118   108        INIT_FCALL                                               'var_dump'
        109        INIT_METHOD_CALL                                         !3, 'getHash'
        110        DO_FCALL                                      0  $56     
        111        SEND_VAR                                                 $56
        112        DO_ICALL                                                 
  119   113        INIT_FCALL                                               'var_dump'
        114        INIT_METHOD_CALL                                         !3, 'getSuper'
        115        DO_FCALL                                      0  $58     
        116        INIT_METHOD_CALL                                         $58, 'getHash'
        117        DO_FCALL                                      0  $59     
        118        SEND_VAR                                                 $59
        119        DO_ICALL                                                 
  120   120        ASSERT_CHECK                                             
        121        INIT_FCALL                                               'assert'
        122        INIT_METHOD_CALL                                         !3, 'getSuper'
        123        DO_FCALL                                      0  $61     
        124        IS_IDENTICAL                                     ~62     !3, $61
        125        SEND_VAL                                                 ~62
        126        SEND_VAL                                                 'assert%28%24immutable+%3D%3D%3D+%24immutable-%3EgetSuper%28%29%29'
        127        DO_ICALL                                                 
  121   128        ASSERT_CHECK                                             
        129        INIT_FCALL                                               'assert'
        130        INIT_METHOD_CALL                                         !3, 'equals'
        131        INIT_METHOD_CALL                                         !3, 'getSuper'
        132        DO_FCALL                                      0  $64     
        133        SEND_VAR_NO_REF_EX                                       $64
        134        DO_FCALL                                      0  $65     
        135        SEND_VAR                                                 $65
        136        SEND_VAL                                                 'assert%28%24immutable-%3Eequals%28%24immutable-%3EgetSuper%28%29%29%29'
        137        DO_ICALL                                                 
        138      > RETURN                                                   1

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

End of function hashcode

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

End of function __construct

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

End of function getname

Function setsuper:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HVcam
function name:  setSuper
number of ops:  4
compiled vars:  !0 = $super
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV_INIT                                        !0      null
   22     1        ASSIGN_OBJ                                               'super'
          2        OP_DATA                                                  !0
   23     3      > RETURN                                                   null

End of function setsuper

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

End of function getsuper

Function gethash:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HVcam
function name:  getHash
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   INIT_FCALL                                               'hashcode'
          1        INIT_FCALL                                               'spl_object_hash'
          2        FETCH_THIS                                       ~0      
          3        SEND_VAL                                                 ~0
          4        DO_ICALL                                         $1      
          5        SEND_VAR                                                 $1
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
   31     8*     > 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/HVcam
function name:  equals
number of ops:  8
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   34     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
   35     7*     > RETURN                                                   null

End of function equals

End of class SomethingBuilder.

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

End of function __construct

End of class Something@anonymous.

Class Something:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 77) Position 1 = 39, Position 2 = 50
Branch analysis from position: 39
2 jumps found. (Code = 78) Position 1 = 40, Position 2 = 50
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 8
Branch analysis from position: 38
Branch analysis from position: 8
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 8
Branch analysis from position: 38
Branch analysis from position: 8
filename:       /in/HVcam
function name:  __construct
number of ops:  52
compiled vars:  !0 = $builder, !1 = $argBuilder, !2 = $immutables, !3 = $builders, !4 = $immutable, !5 = $builderHash
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
   44     1        INIT_METHOD_CALL                                         !0, 'getName'
          2        DO_FCALL                                      0  $7      
          3        ASSIGN_OBJ                                               'name'
          4        OP_DATA                                                  $7
   45     5        ASSIGN                                                   !1, !0
   47     6        ASSIGN                                                   !2, <array>
   48     7        ASSIGN                                                   !3, <array>
   50     8    >   INIT_METHOD_CALL                                         !0, 'getHash'
          9        DO_FCALL                                      0  $11     
         10        ISSET_ISEMPTY_DIM_OBJ                         0          !2, $11
         11      > JMPZ                                                     ~12, ->13
         12    > > JMP                                                      ->38
   52    13    >   IS_IDENTICAL                                             !0, !1
         14      > JMPZ                                                     ~13, ->18
         15    >   FETCH_THIS                                       ~14     
         16        QM_ASSIGN                                        ~15     ~14
         17      > JMP                                                      ->25
         18    >   DECLARE_ANON_CLASS                               <unknown> 'something'
         19        NEW                                              $17     $16
         20        INIT_METHOD_CALL                                         !0, 'getName'
         21        DO_FCALL                                      0  $18     
         22        SEND_VAR_NO_REF_EX                                       $18
         23        DO_FCALL                                      0          
         24        QM_ASSIGN                                        ~15     $17
         25    >   ASSIGN                                                   !4, ~15
   57    26        INIT_METHOD_CALL                                         !0, 'getHash'
         27        DO_FCALL                                      0  $21     
         28        ASSIGN_DIM                                               !2, $21
         29        OP_DATA                                                  !4
   58    30        INIT_METHOD_CALL                                         !0, 'getHash'
         31        DO_FCALL                                      0  $23     
         32        ASSIGN_DIM                                               !3, $23
         33        OP_DATA                                                  !0
   60    34        INIT_METHOD_CALL                                         !0, 'getSuper'
         35        DO_FCALL                                      0  $25     
         36        ASSIGN                                           ~26     !0, $25
         37      > JMPNZ                                                    ~26, ->8
   62    38    > > FE_RESET_R                                       $27     !2, ->50
         39    > > FE_FETCH_R                                       ~28     $27, !4, ->50
         40    >   ASSIGN                                                   !5, ~28
   63    41        FETCH_DIM_R                                      ~31     !3, !5
         42        INIT_METHOD_CALL                                         ~31, 'getSuper'
         43        DO_FCALL                                      0  $32     
         44        INIT_METHOD_CALL                                         $32, 'getHash'
         45        DO_FCALL                                      0  $33     
         46        FETCH_DIM_R                                      ~34     !2, $33
         47        ASSIGN_OBJ                                               !4, 'super'
         48        OP_DATA                                                  ~34
   62    49      > JMP                                                      ->39
         50    >   FE_FREE                                                  $27
   65    51      > RETURN                                                   null

End of function __construct

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

End of function getname

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

End of function getsuper

Function gethash:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HVcam
function name:  getHash
number of ops:  28
compiled vars:  !0 = $origin
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   RECV_INIT                                        !0      null
   76     1        FETCH_THIS                                       ~1      
          2        IS_IDENTICAL                                             !0, ~1
          3      > JMPZ                                                     ~2, ->9
   77     4    >   INIT_FCALL                                               'hashcode'
          5        SEND_VAL                                                 '%2ARECURSION%2A'
          6        DO_FCALL                                      0  $3      
          7      > RETURN                                                   $3
          8*       JMP                                                      ->27
   79     9    >   INIT_FCALL                                               'hashcode'
   82    10        INIT_METHOD_CALL                                         'getName'
         11        DO_FCALL                                      0  $4      
         12        CONCAT                                           ~5      'Something%7B', $4
   83    13        CONCAT                                           ~6      ~5, '%7C'
         14        INIT_METHOD_CALL                                         'getSuper'
         15        DO_FCALL                                      0  $7      
         16        INIT_METHOD_CALL                                         $7, 'getHash'
         17        COALESCE                                         ~8      !0
         18        FETCH_THIS                                       ~9      
         19        QM_ASSIGN                                        ~8      ~9
         20        SEND_VAL_EX                                              ~8
         21        DO_FCALL                                      0  $10     
         22        CONCAT                                           ~11     ~6, $10
   84    23        CONCAT                                           ~12     ~11, '%7D'
         24        SEND_VAL                                                 ~12
         25        DO_FCALL                                      0  $13     
         26      > RETURN                                                   $13
   87    27*     > 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/HVcam
function name:  equals
number of ops:  8
compiled vars:  !0 = $other
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   RECV                                             !0      
   90     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
   91     7*     > RETURN                                                   null

End of function equals

End of class Something.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.16 ms | 1431 KiB | 22 Q