Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Alias Replace Class

When a class is defined before an alias, with use, it used to yield a fatal error. Since PHP 8.4, and when the alias is in a different block than the definition, it is possible to replace a class with another one.

While the fatal error has been removed, it now means that a class, local to a namespace, is not always described by its relative name. The class is still distinguisable with its absolute name.

PHP code

<?php

namespace A {
        class xBefore {}
}

namespace A {
    use y as xAfter;
    use y as xBefore;
    class y {}

  print get_class(new y);    
}


namespace A {
        class xAfter {}
}

?>

Before

PHP Fatal error:  Cannot use y as xBefore because the name is already in use s/aliasReplace.php on line 10

Fatal error: Cannot use y as xBefore because the name is already in use s/aliasReplace.php on line 10

After

A\y

PHP version change

This behavior changed in 8.4.

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat