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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。