3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Can I create an alias for an entire namespace in PHP? * * class_alias() on spl_autoload_class() for namespace prefix * * @link https://stackoverflow.com/a/74236530/367456 * @author hakre <https://hakre.wordpress.com/> */ namespace { // warning instead of exception to simulate testing mode on 3v4l.org, // in development it should throw so that code touching path is // updated. ini_set('assert.exception', '0'); spl_autoload_register( static function (string $class_name) { static $old = 'My\\Old\\Lib\\Name\\'; static $new = 'My\\Tiger\\Lib\\Name\\'; // only operate on the old namespace if (0 !== strpos($class_name, $old)) { return; } $new_name = str_replace($old, $new, $class_name); assert(false, "old namespace in use: $class_name"); class_alias($new_name, $class_name); $exists = class_exists($new_name); assert($exists, "aliasing old namespace failed for class: $class_name"); }, $throw = true, /* throw when registering fails */ $prepend = false /* only alias at fall-through */ ); } namespace My\Tiger\Lib\Name\Module { class Boring { } } namespace My\Old\Lib\Name { use My\Old\Lib\Name\Module\Boring; $boring = new Boring(); assert($boring); assert($boring instanceof Module\Boring); var_dump(get_class($boring)); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/k2SDE
function name:  (null)
number of ops:  33
compiled vars:  !0 = $throw, !1 = $prepend, !2 = $boring
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   INIT_FCALL                                               'ini_set'
          1        SEND_VAL                                                 'assert.exception'
          2        SEND_VAL                                                 '0'
          3        DO_ICALL                                                 
   18     4        INIT_FCALL                                               'spl_autoload_register'
   19     5        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
   32     6        SEND_VAL                                                 ~4
   33     7        ASSIGN                                           ~5      !0, <true>
          8        SEND_VAL                                                 ~5
   34     9        ASSIGN                                           ~6      !1, <false>
         10        SEND_VAL                                                 ~6
   18    11        DO_ICALL                                                 
   46    12        NEW                                              $8      'My%5COld%5CLib%5CName%5CModule%5CBoring'
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !2, $8
   47    15        ASSERT_CHECK                                             
         16        INIT_NS_FCALL_BY_NAME                                    'My%5COld%5CLib%5CName%5Cassert'
         17        SEND_VAR_EX                                              !2
         18        SEND_VAL_EX                                              'assert%28%24boring%29'
         19        DO_FCALL                                      0          
   48    20        ASSERT_CHECK                                             
         21        INIT_NS_FCALL_BY_NAME                                    'My%5COld%5CLib%5CName%5Cassert'
         22        INSTANCEOF                                       ~12     !2, 'My%5COld%5CLib%5CName%5CModule%5CBoring'
         23        SEND_VAL_EX                                              ~12
         24        SEND_VAL_EX                                              'assert%28%24boring+instanceof+Module%5CBoring%29'
         25        DO_FCALL                                      0          
   49    26        INIT_NS_FCALL_BY_NAME                                    'My%5COld%5CLib%5CName%5Cvar_dump'
         27        INIT_NS_FCALL_BY_NAME                                    'My%5COld%5CLib%5CName%5Cget_class'
         28        SEND_VAR_EX                                              !2
         29        DO_FCALL                                      0  $14     
         30        SEND_VAR_NO_REF_EX                                       $14
         31        DO_FCALL                                      0          
   50    32      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/k2SDE
function name:  {closure}
number of ops:  39
compiled vars:  !0 = $class_name, !1 = $old, !2 = $new, !3 = $new_name, !4 = $exists
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   20     1        BIND_STATIC                                              !1
   21     2        BIND_STATIC                                              !2
   24     3        INIT_FCALL                                               'strpos'
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $5      
          7        IS_NOT_IDENTICAL                                         $5, 0
          8      > JMPZ                                                     ~6, ->10
   25     9    > > RETURN                                                   null
   27    10    >   INIT_FCALL                                               'str_replace'
         11        SEND_VAR                                                 !1
         12        SEND_VAR                                                 !2
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $7      
         15        ASSIGN                                                   !3, $7
   28    16        ASSERT_CHECK                                             
         17        INIT_FCALL                                               'assert'
         18        SEND_VAL                                                 <false>
         19        NOP                                                      
         20        FAST_CONCAT                                      ~9      'old+namespace+in+use%3A+', !0
         21        SEND_VAL                                                 ~9
         22        DO_ICALL                                                 
   29    23        INIT_FCALL                                               'class_alias'
         24        SEND_VAR                                                 !3
         25        SEND_VAR                                                 !0
         26        DO_ICALL                                                 
   30    27        INIT_FCALL                                               'class_exists'
         28        SEND_VAR                                                 !3
         29        DO_ICALL                                         $12     
         30        ASSIGN                                                   !4, $12
   31    31        ASSERT_CHECK                                             
         32        INIT_FCALL                                               'assert'
         33        SEND_VAR                                                 !4
         34        NOP                                                      
         35        FAST_CONCAT                                      ~14     'aliasing+old+namespace+failed+for+class%3A+', !0
         36        SEND_VAL                                                 ~14
         37        DO_ICALL                                                 
   32    38      > RETURN                                                   null

End of Dynamic Function 0

Class My\Tiger\Lib\Name\Module\Boring: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.69 ms | 1016 KiB | 22 Q