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


PHP ReflectionMethod __toString()用法及代碼示例


ReflectionMethod::__toString()函數是PHP中的一個內置函數,用於返回指定方法對象的字符串表示形式。

用法:

string ReflectionMethod::__toString ( void )

參數:該函數不接受任何參數。


返回值:此函數返回指定方法對象的字符串表示形式。

以下示例程序旨在說明PHP中的ReflectionMethod::__toString()函數:程序1:

<?php 
  
// Initializing a user-defined class 
class Company { 
  
     function GFG() {} 
} 
  
// Using ReflectionMethod() over the class Company 
$A = new ReflectionMethod('Company', 'GFG'); 
  
// Calling the __toString() function 
$B = $A->__toString(); 
  
// Getting the string representation of  
// the specified method object 'GFG' 
var_dump($B); 
?>

輸出:

string(94) "Method [ <user> public method GFG ] {
  @@ /home/43d46426def0fce620e2e2cf2bf10e7a.php 6 - 6
}
"

程序2:

<?php 
   
// Initializing some user-defined classes 
class Department1 { 
   
    private function hr($name) { 
        return 'HR' . $name; 
    } 
} 
class Department2 { 
   
    static function Coding() {} 
} 
  
class Department3 { 
   
     protected function marketing(){} 
} 
  
// Using ReflectionMethod() over the above classes 
$A = new ReflectionMethod('Department1', 'hR'); 
$B = new ReflectionMethod('Department2', 'Coding'); 
$C = new ReflectionMethod('Department3', 'marketing'); 
   
// Calling the __toString() function and  
// getting the string representation of the 
// above method objects 
var_dump($A->__toString()); 
var_dump($B->__toString()); 
var_dump($C->__toString()); 
?>

輸出:

string(158) "Method [ <user> private method hr ] {
  @@ /home/2aa9608c9056b85288d53e96d9de3310.php 6 - 8

  - Parameters [1] {
    Parameter #0 [ <required> $name ]
  }
}
"
string(106) "Method [ <user> static public method Coding ] {
  @@ /home/2aa9608c9056b85288d53e96d9de3310.php 12 - 12
}
"
string(105) "Method [ <user> protected method marketing ] {
  @@ /home/2aa9608c9056b85288d53e96d9de3310.php 17 - 17
}
"

參考: https://www.php.net/manual/en/reflectionmethod.tostring.php



相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 PHP | ReflectionMethod __toString() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。