PHP class_alias() Function
The PHP class_alias() function Creates an alias for a user defined class. The aliased class is exactly the same as the original class.
Syntax
class_alias(class, alias, autoload)
Parameters
class |
Required. Specify the original class name. |
class |
Required. Specify the alias name for the class. |
autoload |
Optional. Specify whether to call __autoload if the original class is not found. |
Return Value
Returns true on success or false on failure.
Example: class_alias() example
The example below shows the usage of class_alias() function.
<?php class myClass { function message() { echo "Hello World\n"; } } //creating an alias of the class class_alias('myClass', 'newClass'); //creating an object $myObj = new newClass(); //calling method $myObj->message(); ?>
The output of the above code will be:
Hello World
Example: comparing original class and alias class
Consider one more example where the original class is compared with alias class.
<?php class foo { } class_alias('foo', 'bar'); $a = new foo(); $b = new bar(); //comparing class objects var_dump($a == $b, $a === $b); var_dump($a instanceof $b); //comparing classes var_dump($a instanceof foo); var_dump($a instanceof bar); var_dump($b instanceof foo); var_dump($b instanceof bar); ?>
The output of the above code will be:
bool(true) bool(false) bool(true) bool(true) bool(true) bool(true) bool(true)
❮ PHP Classes/Objects Reference