The class_alias() function is an inbuilt function in PHP which is used to create an alias name of the class. The functionality of the aliased class is similar to the original class.
Syntax:
php
php
bool class_alias( string $original, string $alias, bool $autoload = TRUE )Parameters: This function accepts three parameters as mentioned above and described below:
- $original: This parameter holds the original class name.
- $alias: This parameter holds the alias class name.
- $autoload: It is autoload or not if original class is not found.
<?php
// Create a class
class GFG {
public $Geek_name = "Welcome to GeeksforGeeks";
// Constructor is being implemented.
public function __construct($Geek_name) {
$this->Geek_name = $Geek_name;
}
}
// Create the class name alias
class_alias('GFG', 'GeeksforGeeks');
// Create an object
$Geek = new GeeksforGeeks("GeeksforGeeks");
// Display result
echo $Geek->Geek_name;
?>
Output:
Program 2:
GeeksforGeeks
<?php
// Creating class
class GFG {
public $data1;
public $data2;
public $data3;
}
// Create the class name alias
class_alias('GFG', 'Geeks');
// Creating an object
$obj1 = new GFG();
$obj2 = new Geeks();
var_dump($obj1 === $obj2);
// Set values of $obj object
$obj2->data1 = "Geeks";
$obj2->data2 = "for";
$obj2->data3 = "Geeks";
// Print values of $obj object
echo "$obj2->data1 \n$obj2->data2 \n$obj2->data3";
?>
Output:
Reference: https://www.php.net/manual/en/function.class-alias.phpbool(false) Geeks for Geeks