当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP ReflectionMethod getPrototype()用法及代码示例


ReflectionMethod::getPrototype()函数是PHP中的内置函数,用于返回指定方法的原型,否则,如果该方法没有原型,则返回异常/错误。

用法:

ReflectionMethod ReflectionMethod::getPrototype( void )

参数:该函数不接受任何参数。


返回值:该函数返回指定方法的原型,否则,如果该方法没有原型,则返回异常/错误。

以下示例程序旨在说明PHP中的ReflectionMethod::getPrototype()函数:
程序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); 
?>

输出:

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

程序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); 
?>

输出:方法没有任何原型,它将给您一个错误。

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

参考: https://www.php.net/manual/en/reflectionmethod.getprototype.php



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 PHP | ReflectionMethod getPrototype() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。