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


PHP ReflectionClass getStaticProperties()用法及代碼示例


ReflectionClass::getStaticProperties()函數是PHP中的內置函數,用於返回靜態屬性數組。該函數當前未記錄,但其參數列表可用。

用法:

ReflectionClass::getStaticProperties(void) : array

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


返回值:此函數返回靜態屬性的數組。

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

<?php 
   
// Defining a class named as Departments 
class Departments { 
    public $Dept1 = 'CSE'; 
    private $Dept2 = 'ECE'; 
    public static $Dept3 = 'EE'; 
} 
   
// Using ReflectionClass over the class Departments 
$ReflectionClass = new ReflectionClass('Departments'); 
   
// Calling getStaticProperties() function 
$A = $ReflectionClass->getStaticProperties(); 
   
// Getting an array of the static properties 
var_dump($A); 
?>

輸出:

array(1) {
  ["Dept3"]=>
  string(2) "EE"
}

示例2:

<?php 
  
// Defining a class named as Departments 
class GFG { 
    static $name = "GeeksforGeeks"; 
    private static $addr = "Noida"; 
    protected static $email = 'abc@geeksforgeeks.org'; 
} 
  
// Using ReflectionClass  
$ReflectionClass = new ReflectionClass('GFG'); 
  
// Calling getStaticProperties() function 
$Property = $ReflectionClass->getStaticProperties(); 
  
// Getting an array of the static properties 
// if present 
var_dump($Property); 
?>

輸出:

array(3) {
  ["name"]=>
  string(13) "GeeksforGeeks"
  ["addr"]=>
  string(5) "Noida"
  ["email"]=>
  string(21) "abc@geeksforgeeks.org"
}

參考: https://www.php.net/manual/en/reflectionclass.getstaticproperties.php



相關用法


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