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
class GFG {
public $Geek_name = "Welcome to GeeksforGeeks" ;
public function __construct( $Geek_name ) {
$this ->Geek_name = $Geek_name ;
}
}
class_alias( 'GFG' , 'GeeksforGeeks' );
$Geek = new GeeksforGeeks( "GeeksforGeeks" );
echo $Geek ->Geek_name;
?>
|
Program 2:
<?php
class GFG {
public $data1 ;
public $data2 ;
public $data3 ;
}
class_alias( 'GFG' , 'Geeks' );
$obj1 = new GFG();
$obj2 = new Geeks();
var_dump( $obj1 === $obj2 );
$obj2 ->data1 = "Geeks" ;
$obj2 ->data2 = "for" ;
$obj2 ->data3 = "Geeks" ;
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
Share your thoughts in the comments
Please Login to comment...