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


PHP ReflectionClass getStaticPropertyValue()用法及代码示例


ReflectionClass::getStaticPropertyValue()函数是PHP中的内置函数,用于返回静态属性的值。

用法:

mixed ReflectionClass::getStaticPropertyValue( string $name, mixed &$def_value )

参数:此函数接受单个参数名称,该名称是指定静态属性的名称。


返回值:此函数返回静态属性的值。

以下示例程序旨在说明PHP中的ReflectionClass::getStaticPropertyValue()函数:

示例1:

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

输出:

string(2) "EE"

示例2:

<?php 
   
// Defining a class named as Departments 
class Departments { 
    static $Dept1 = 'CSE'; 
    static $Dept2 = 'ECE'; 
    public static $Dept3 = 'EE'; 
} 
   
// Using ReflectionClass over the class Departments 
$ReflectionClass = new ReflectionClass('Departments'); 
   
// Calling getStaticPropertyValue() functions 
$A = $ReflectionClass->getStaticPropertyValue('Dept1'); 
$B = $ReflectionClass->getStaticPropertyValue('Dept2'); 
$C = $ReflectionClass->getStaticPropertyValue('Dept3'); 
   
// Getting the value of the static property. 
var_dump($A); 
var_dump($B); 
var_dump($C); 
?>

输出:

string(3) "CSE"
string(3) "ECE"
string(2) "EE"

参考: https://www.php.net/manual/en/reflectionclass.getstaticpropertyvalue.php



相关用法


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