Friday, November 14, 2025
HomeLanguagesPHP | ReflectionMethod getPrototype() Function

PHP | ReflectionMethod getPrototype() Function

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 neveropen($name) {
        return 'GFG' . $name;
    }
}
  
// Using ReflectionMethod() over the class Company
$A = new ReflectionMethod('Company', 'neveropen');
  
// 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::
neveropen 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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS