3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait EvilTrait { /** * @var int */ public $foobarCalls = 0; /** * final AND protected, even if you override it, you shouldn't be allowed to make it private, right? * @return string */ final protected function getSomeString() { return 'This was returned by a protected method called getSomeString'; } /** * Abstracts are useful to ensure certain methods exist, and have a particular signature */ abstract protected function someAbstractMethod(); /** * Be careful with method names like this, if the using class has the same name, it might become * an old-school constructor... Oh, did you know that PHP4-style constructors emit E_DEPRECATED notices? */ public function foobar() { $this->foobarCalls++; } } class Foobar { //using an alias AND changing visibility, hmmm... that may not be a good idea use EvilTrait { getSomeString as public iSeeYou; } /** * Abstract declares this method as protected, so only protected and public * should be allowed, this is private, I'd expect a fatal error here: */ private function someAbstractMethod() { return 'This should not be allowed'; } /** * We've implemented an abstract method in an illegal fashion * Let's check if we can call this contract-breaking implementation: */ public function testAbstractImplementation() { return $this->someAbstractMethod(); } /** * Note: using as <alias> doesn't mean the original method-name is forgotten * It only means that you can call it by a different name, AND that its visibility * depends on the alias... $x->getSomeString() doesn't work */ public function demonstrateAlias() { return $this->getSomeString(); } } $x = new Foobar(); var_dump($x->foobarCalls); echo $x->testAbstractImplementation(), PHP_EOL, $x->iSeeYou(), PHP_EOL, $x->demonstrateAlias();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mcJR2
function name:  (null)
number of ops:  20
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   DECLARE_CLASS                                            'foobar'
   65     1        NEW                                              $1      'Foobar'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   66     4        INIT_FCALL                                               'var_dump'
          5        FETCH_OBJ_R                                      ~4      !0, 'foobarCalls'
          6        SEND_VAL                                                 ~4
          7        DO_ICALL                                                 
   67     8        INIT_METHOD_CALL                                         !0, 'testAbstractImplementation'
          9        DO_FCALL                                      0  $6      
         10        ECHO                                                     $6
         11        ECHO                                                     '%0A'
   68    12        INIT_METHOD_CALL                                         !0, 'iSeeYou'
         13        DO_FCALL                                      0  $7      
         14        ECHO                                                     $7
         15        ECHO                                                     '%0A'
   69    16        INIT_METHOD_CALL                                         !0, 'demonstrateAlias'
         17        DO_FCALL                                      0  $8      
         18        ECHO                                                     $8
         19      > RETURN                                                   1

Class EvilTrait:
Function getsomestring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mcJR2
function name:  getSomeString
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E > > RETURN                                                   'This+was+returned+by+a+protected+method+called+getSomeString'
   16     1*     > RETURN                                                   null

End of function getsomestring

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

End of function someabstractmethod

Function foobar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mcJR2
function name:  foobar
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   PRE_INC_OBJ                                              'foobarCalls'
   29     1      > RETURN                                                   null

End of function foobar

End of class EvilTrait.

Class Foobar:
Function someabstractmethod:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mcJR2
function name:  someAbstractMethod
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E > > RETURN                                                   'This+should+not+be+allowed'
   43     1*     > RETURN                                                   null

End of function someabstractmethod

Function testabstractimplementation:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mcJR2
function name:  testAbstractImplementation
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   INIT_METHOD_CALL                                         'someAbstractMethod'
          1        DO_FCALL                                      0  $0      
          2      > RETURN                                                   $0
   52     3*     > RETURN                                                   null

End of function testabstractimplementation

Function demonstratealias:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mcJR2
function name:  demonstrateAlias
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   INIT_METHOD_CALL                                         'getSomeString'
          1        DO_FCALL                                      0  $0      
          2      > RETURN                                                   $0
   62     3*     > RETURN                                                   null

End of function demonstratealias

End of class Foobar.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.76 ms | 1400 KiB | 15 Q