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


PHP ReflectionProperty getDeclaringClass()用法及代码示例


ReflectionProperty::getDeclaringClass()函数是PHP中的内置函数,用于返回指定的声明类。

用法:

ReflectionClass ReflectionProperty::getDeclaringClass ( void )

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


返回值:此函数返回指定的声明类。

以下示例程序旨在说明PHP中的ReflectionProperty::getDeclaringClass()函数:程序1:

<?php 
  
// Initializing a user-defined class Company 
class Company { 
    public $SizeOfGeeksforGeeks = 13; 
    public $SizeOfGFG = 3; 
} 
  
// Using ReflectionProperty  
$A = new ReflectionProperty('Company', 'SizeOfGeeksforGeeks'); 
$B = new ReflectionProperty('Company', 'SizeOfGFG'); 
  
// Calling the getDeclaringClass() function 
$C = $A->getDeclaringClass(); 
$D = $B->getDeclaringClass(); 
  
// Getting the specified declaring classes 
var_dump($C); 
var_dump($D); 
?>

输出:

object(ReflectionClass)#3 (1) {
  ["name"]=>
  string(7) "Company"
}
object(ReflectionClass)#4 (1) {
  ["name"]=>
  string(7) "Company"
}

程序2:

<?php 
  
// Initializing some user-defined classes 
class Department1 
{ 
    public $SizeOfHR = 2; 
} 
class Department2 
{ 
    public $SizeOfCoding = 6; 
} 
class Department3 
{ 
    public $SizeOfMarketing = 9; 
} 
  
// Using ReflectionProperty over above classes 
$A = new ReflectionProperty('Department1', 'SizeOfHR'); 
$B = new ReflectionProperty('Department2', 'SizeOfCoding'); 
$C = new ReflectionProperty('Department3', 'SizeOfMarketing'); 
  
// Calling the getDeclaringClass() function 
$D = $A->getDeclaringClass(); 
$E = $B->getDeclaringClass(); 
$F = $C->getDeclaringClass(); 
  
// Getting the specified declaring classes 
var_dump($D); 
var_dump($E); 
var_dump($F); 
?>

输出:

object(ReflectionClass)#4 (1) {
  ["name"]=>
  string(11) "Department1"
}
object(ReflectionClass)#5 (1) {
  ["name"]=>
  string(11) "Department2"
}
object(ReflectionClass)#6 (1) {
  ["name"]=>
  string(11) "Department3"
}

参考: https://www.php.net/manual/en/reflectionproperty.getdeclaringclass.php



相关用法


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