Open In App

PHP | class_alias() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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:

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.

Return Value: It returns Boolean value i.e. either True on success or False on failure.

Below programs illustrate the class_alias() function in PHP:

Program 1:




<?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:

GeeksforGeeks

Program 2:




<?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:

bool(false)
Geeks  
for  
Geeks

Reference: https://www.php.net/manual/en/function.class-alias.php



Last Updated : 16 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads