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


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