首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

ReflectionProperty::getValue

(PHP 5, PHP 7)

ReflectionProperty :: getValue - 获取值

描述

代码语言:javascript
复制
public mixed ReflectionProperty::getValue ([ object $object ] )

获取属性的值。

参数

object

如果该属性是非静态的,则必须提供一个对象来从中获取属性。如果您想在不提供对象的情况下获取默认属性,请改用ReflectionClass :: getDefaultProperties()。

返回值

该属性的当前值。

错误/异常

如果属性不可访问,则抛出ReflectionException。您可以使用ReflectionProperty :: setAccessible()来访问保护或私有属性。

例子

示例#1 ReflectionProperty :: getValue()示例

代码语言:javascript
复制
<?php
class Foo {
    public static $staticProperty = 'foobar';
    
    public $property = 'barfoo';
    protected $privateProperty = 'foofoo';
}

$reflectionClass = new ReflectionClass('Foo');

var_dump($reflectionClass->getProperty('staticProperty')->getValue());
var_dump($reflectionClass->getProperty('property')->getValue(new Foo));

$reflectionProperty = $reflectionClass->getProperty('privateProperty');
$reflectionProperty->setAccessible(true);
var_dump($reflectionProperty->getValue(new Foo));
?>

上面的例子将输出:

代码语言:javascript
复制
string(6) "foobar"
string(6) "barfoo"
string(6) "foofoo"

← ReflectionProperty::getName

ReflectionProperty::isDefault →

扫码关注腾讯云开发者

领取腾讯云代金券