Sunday, November 17, 2024
Google search engine
HomeLanguagesPHP | class_alias() Function

PHP | class_alias() Function

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 neveropen"
      
    // Constructor is being implemented. 
    public function __construct($Geek_name) { 
        $this->Geek_name = $Geek_name
    
  
// Create the class name alias
class_alias('GFG', 'neveropen');
  
// Create an object
$Geek = new neveropen("neveropen"); 
  
// Display result
echo $Geek->Geek_name; 
?>


Output:

neveropen

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

RELATED ARTICLES

Most Popular

Recent Comments