3v4l.org

run code in 500+ 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 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/k2SDE
function name:  {closure:/in/k2SDE:19}
number of ops:  31
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        FRAMELESS_ICALL_2                strpos              ~5      !0, !1
          4        IS_NOT_IDENTICAL                                             ~5, 0
          5      > JMPZ                                                         ~6, ->7
   25     6    > > RETURN                                                       null
   27     7    >   FRAMELESS_ICALL_3                str_replace         ~7      !1, !2
          8        OP_DATA                                                      !0
          9        ASSIGN                                                       !3, ~7
   28    10        ASSERT_CHECK                                                 
         11        INIT_FCALL                                                   'assert'
         12        SEND_VAL                                                     <false>
         13        NOP                                                          
         14        FAST_CONCAT                                          ~9      'old+namespace+in+use%3A+', !0
         15        SEND_VAL                                                     ~9
         16        DO_ICALL                                                     
   29    17        INIT_FCALL                                                   'class_alias'
         18        SEND_VAR                                                     !3
         19        SEND_VAR                                                     !0
         20        DO_ICALL                                                     
   30    21        FRAMELESS_ICALL_1                class_exists        ~12     !3
         22        ASSIGN                                                       !4, ~12
   31    23        ASSERT_CHECK                                                 
         24        INIT_FCALL                                                   'assert'
         25        SEND_VAR                                                     !4
         26        NOP                                                          
         27        FAST_CONCAT                                          ~14     'aliasing+old+namespace+failed+for+class%3A+', !0
         28        SEND_VAL                                                     ~14
         29        DO_ICALL                                                     
   32    30      > 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.5.0


preferences:
169.18 ms | 1902 KiB | 19 Q