In PHP, the class_alias
function is used to create an alias or alternate name for an existing class. The aliased class works the same as the original class. This can be particularly useful in situations where you want to provide a more intuitive or simplified name for a class, especially when dealing with third-party libraries or frameworks. It also can be helpful to maintain backward compatibility while renaming class.
Since PHP 8.3, it is possible to alias built-in PHP classes and interfaces.
<?php
class_alias('stdClass', 'NewStdClass');
$obj = new NewStdClass();
print_r($obj); // stdClass Object()
The code uses class_alias
to create an alias, NewStdClass
, for the built-in PHP class stdClass
. Even when using the alias to create an object, it still belongs to the original stdClass
class.
Leave a Comment
Cancel reply