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


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


ReflectionClass::getDefaultProperties()函數是PHP中的內置函數,用於返回默認屬性,包括從指定類繼承的屬性。

用法:

ReflectionClass::getDefaultProperties(void) : array

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


返回值:此函數返回默認屬性的數組。這些屬性為屬性的鍵及其值留有空間。鍵是屬性的名稱,值是屬性的默認值。如果該屬性沒有任何默認值,它也將返回NULL。

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

<?php 
  
// Defining a class named as College 
class College { 
      
    // Defining a protected property 
    protected $College_Name = 'IIT Delhi'; 
} 
  
// Defining a sub class Departments of the  
// base class College 
class Departments extends College { 
    public $Dept1 = 'CSE'; 
    private $Dept2 = 'ECE'; 
    public static $Dept3 = 'EE'; 
} 
  
// Using ReflectionClass over sub class Departments 
$ReflectionClass = new ReflectionClass('Departments'); 
  
// Getting an array of the default properties 
var_dump($ReflectionClass->getDefaultProperties()); 
?>

輸出:

array(4) {
  ["Dept3"]=>
  string(2) "EE"
  ["Dept1"]=>
  string(3) "CSE"
  ["Dept2"]=>
  string(3) "ECE"
  ["College_Name"]=>
  string(9) "IIT Delhi"
}

示例2:

<?php 
  
// Defining a class named as College 
class College { 
      
    // Defining a protected property 
    protected $College_Name = 'IIT Delhi'; 
} 
  
// Defining a sub class Departments of the  
// base class College 
class Departments extends College { 
    public $Dept1; 
    private $Dept2; 
    public static $Dept3; 
} 
  
// Using ReflectionClass over sub class Departments 
$ReflectionClass = new ReflectionClass('Departments'); 
  
// Getting an array of the default properties 
var_dump($ReflectionClass->getDefaultProperties()); 
?>

輸出:

array(4) {
  ["Dept3"]=>
  NULL
  ["Dept1"]=>
  NULL
  ["Dept2"]=>
  NULL
  ["College_Name"]=>
  string(9) "IIT Delhi"
}

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



相關用法


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