What is PHP class_alias() Function?
If you want to create an alternative name (alias) of an existing class, use class_alias() function. The aliased class acts same as the original class. Please note that it is not a copy of the original class.
Syntax:
class_alias(class, alias, autoload)
Parameters:
The Function has 2 required parameters and 1 optional parameter-
class (Required): It specifies name of a class.
alias (Required): It specifies the alas name of the class.
autoload (Optional): It determines whether to call the autoloader if the original class is not found. It can take 2 values-
- TRUE – it is the default value. It indicates that autoloader should be called if the original class is not found.
- FALSE – It indicates that autoloader should not be called if the original class is not found.
Return Values:
The function returns-
- TRUE on success
- FALSE on failure
Examples:
Example:
<?php
class ItIsALongOriginalHumanClassName {
public $Name = "John";
}
class_alias('ItIsALongOriginalHumanClassName', 'HumanAlias');
$humanObj = new HumanAlias();
echo "The person's name is: " . $humanObj->Name;
?>
Output:
The person's name is: John
Practical Usages of class_alias() Function:
It simplifies a long class name to a shorter one.
PHP Version Support:
PHP 5 >= 5.3.0, PHP 7, PHP 8
Summary: PHP class_alias() Function
Class_alias() is a class/object type function in PHP. It allows developers to call a function in its original name or an alternative name interchangeably.