PHP | ReflectionMethod getPrototype() Function
Last Updated :
23 Dec, 2019
The ReflectionMethod::getPrototype() function is an inbuilt function in PHP which is used to return the prototype of the specified method otherwise, an exception/error is returned if the method does not have a prototype.
Syntax:
ReflectionMethod ReflectionMethod::getPrototype( void )
Parameters: This function does not accept any parameter.
Return Value: This function returns the prototype of the specified method otherwise, an exception/error is returned if the method does not have a prototype.
Below programs illustrate the ReflectionMethod::getPrototype() function in PHP:
Program 1:
<?php
class Department1 {
public function Marketing_Dpt() {
return 'Department1' ;
}
}
class Department2 extends Department1{
public function Marketing_Dpt() {
return 'Department2' ;
}
}
class Department3 extends Department2{
public function Coding_Dpt() {
return 'Department3' ;
}
}
$A = new ReflectionMethod( 'Department3' , 'Marketing_Dpt' );
$B = $A ->getPrototype();
var_dump( $B );
?>
|
Output:
object(ReflectionMethod)#2 (2) {
["name"]=>
string(13) "Marketing_Dpt"
["class"]=>
string(11) "Department1"
}
Program 2:
<?php
class Company {
protected function GeeksforGeeks( $name ) {
return 'GFG' . $name ;
}
}
$A = new ReflectionMethod( 'Company' , 'GeeksforGeeks' );
$B = $A ->getPrototype();
var_dump( $B );
?>
|
Output: Method does not have any prototype that will give you an error.
PHP Fatal error: Uncaught ReflectionException: Method Company::
GeeksforGeeks does not have a prototype in
/home/9d8367b263ee4d7ec6941876b0eae792.php:15
Stack trace:
#0 /home/9d8367b263ee4d7ec6941876b0eae792.php(15):
ReflectionMethod->getPrototype()
#1 {main}
thrown in /home/9d8367b263ee4d7ec6941876b0eae792.php on line 15
Reference: https://www.php.net/manual/en/reflectionmethod.getprototype.php
Share your thoughts in the comments
Please Login to comment...