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


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


ReflectionProperty::__toString()函數是PHP中的內置函數,用於返回指定屬性的字符串形式。

用法:

public string ReflectionProperty::__toString ( void ):string

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


返回值:此函數返回指定屬性的字符串形式。

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

<?php 
  
// Initializing a user-defined class Company 
class Company 
{ 
    private $SizeOfGeeksforGeeks = 13; 
    private $SizeOfGFG = 3; 
} 
  
// Using ReflectionProperty  
$A = new ReflectionProperty('Company', 'SizeOfGeeksforGeeks'); 
$B = new ReflectionProperty('Company', 'SizeOfGFG'); 
  
// Calling the __toString() function 
$C = $A->__toString(); 
$D = $B->__toString(); 
  
// Getting the string form of the specified property. 
var_dump($C); 
var_dump($D); 
?>

輸出:

string(52) "Property [ <default> private $SizeOfGeeksforGeeks ]
"
string(42) "Property [ <default> private $SizeOfGFG ]
"

程序2:

<?php 
  
// Initializing some user-defined classes 
class Department1 
{ 
    protected $SizeOfHR; 
} 
class Department2 
{ 
    public $SizeOfCoding = 6; 
} 
class Department3 
{ 
    static $SizeOfMarketing = 9; 
} 
  
// Using ReflectionProperty over above classes 
$A = new ReflectionProperty('Department1', 'SizeOfHR'); 
$B = new ReflectionProperty('Department2', 'SizeOfCoding'); 
$C = new ReflectionProperty('Department3', 'SizeOfMarketing'); 
  
// Calling the __toString() function and 
// getting the string form of the specified property. 
var_dump($A->__toString()); 
var_dump($B->__toString()); 
var_dump($C->__toString()); 
?>

輸出:

string(43) "Property [ <default> protected $SizeOfHR ]
"
string(44) "Property [ <default> public $SizeOfCoding ]
"
string(44) "Property [ public static $SizeOfMarketing ]
"

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



相關用法


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