The class_alias Function

The class_alias function creates an alias for the specified class. The first parameter accepts the name of the original class, the second - the name of the alias. The third optional parameter determines whether the function should be called by autoload.

Syntax

phpclass_alias(original, alias, [autoload]);

Example

Let's create a class and its alias:

php<?php
  class MyClass {
    public function test() {
      return 'Hello';
    }
  }
  
  class_alias('MyClass', 'MyAlias');
  
  $obj = new MyAlias();
  echo $obj->test();
?>

Code execution result:

php'Hello'

Example

Let's check if the class alias exists:

php<?php
  class TestClass {}
  class_alias('TestClass', 'TC');
  
  if (class_exists('TC')) {
    echo 'Alias exists';
  }
?>

Code execution result:

php'Alias exists'

See Also