class_alias()
class_alias() — Creates an alias for a class.
Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.3.0, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Creates an alias named alias based on the user defined class class. The aliased class is exactly the same as the original class.
Signature
class_alias(string $class, string $alias, bool $autoload = true): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $class | string | The original class. |
| $alias | string | The alias name for the class. |
| $autoload | bool | Whether to autoload if the original class is not found. |
Return value
Returns true on success or false on failure.
Example
A runnable snippet:
class Foo { } class_alias('Foo', 'Bar'); $a = new Foo; $b = new Bar; // the objects are the same var_dump($a == $b, $a === $b); var_dump($a instanceof $b); // the classes are the same var_dump($a instanceof Foo); var_dump($a instanceof Bar); var_dump($b instanceof Foo); var_dump($b instanceof Bar);
Output:
bool(true) bool(false) bool(true) bool(true) bool(true) bool(true) bool(true)
Changelog
| Version | Change |
|---|---|
| 8.3.0 | class_alias() now supports creating an alias of an internal class. |
See also
get_parent_class(), is_subclass_of()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page class_alias() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.