Open In App

PHP | ReflectionMethod getPrototype() Function

Last Updated : 23 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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
   
// Initializing a user-defined class Department1
class Department1 {
   
    public function Marketing_Dpt() {
        return 'Department1';
    }
}
   
// Initializing a subclass of the above class Department1
class Department2 extends Department1{
   
    public function Marketing_Dpt() {
        return 'Department2';
    }
   
}
  
// Again Initializing a subclass of the above subclass Department2
class Department3 extends Department2{
   
    public function Coding_Dpt() {
        return 'Department3';
    }
   
}
   
// Using the ReflectionMethod() over the above 
// subclass Department2
$A = new ReflectionMethod('Department3', 'Marketing_Dpt');
   
// Calling the getPrototype() function
$B = $A->getPrototype();
   
// Getting the prototype 
var_dump($B);
?>


Output:

object(ReflectionMethod)#2 (2) {
  ["name"]=>
  string(13) "Marketing_Dpt"
  ["class"]=>
  string(11) "Department1"
}

Program 2:




<?php
  
// Initializing a user-defined class
class Company {
  
    protected function GeeksforGeeks($name) {
        return 'GFG' . $name;
    }
}
  
// Using ReflectionMethod() over the class Company
$A = new ReflectionMethod('Company', 'GeeksforGeeks');
  
// Calling the getPrototype() function
$B = $A->getPrototype();
  
// Getting the prototype or an error is returned
// if the method does not have a prototype.
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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads