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


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