當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。