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

ReflectionProperty::__construct

(PHP 5, PHP 7)

ReflectionProperty :: __构造 - 构造一个ReflectionProperty对象

描述

代码语言:javascript
复制
public ReflectionProperty::__construct ( mixed $class , string $name )

警告

此功能目前没有记录; 只有它的参数列表可用。

参数

class

包含该属性的类名称。

name

所反映的财产的名称。

返回值

没有值返回。

错误/异常

尝试获取或设置私有或受保护的类属性的值将导致引发异常。

例子

Example#1 ReflectionProperty :: __ construct()示例

代码语言:javascript
复制
<?php
class Str
{
    public $length  = 5;
}

// Create an instance of the ReflectionProperty class
$prop = new ReflectionProperty('Str', 'length');

// Print out basic information
printf(
    "===> The%s%s%s%s property '%s' (which was %s)\n" .
    "     having the modifiers %s\n",
        $prop->isPublic() ? ' public' : '',
        $prop->isPrivate() ? ' private' : '',
        $prop->isProtected() ? ' protected' : '',
        $prop->isStatic() ? ' static' : '',
        $prop->getName(),
        $prop->isDefault() ? 'declared at compile-time' : 'created at run-time',
        var_export(Reflection::getModifierNames($prop->getModifiers()), 1)
);

// Create an instance of Str
$obj= new Str();

// Get current value
printf("---> Value is: ");
var_dump($prop->getValue($obj));

// Change value
$prop->setValue($obj, 10);
printf("---> Setting value to 10, new value is: ");
var_dump($prop->getValue($obj));

// Dump object
var_dump($obj);
?>

上面的例子会输出类似于:

代码语言:javascript
复制
===> The public property 'length' (which was declared at compile-time)
     having the modifiers array (
  0 => 'public',
)
---> Value is: int(5)
---> Setting value to 10, new value is: int(10)
object(Str)#2 (1) {
  ["length"]=>
  int(10)
}

示例#2使用 ReflectionProperty类从私有和受保护属性获取值

代码语言:javascript
复制
<?php

class Foo {
    public $x = 1;
    protected $y = 2;
    private $z = 3;
}

$obj = new Foo;

$prop = new ReflectionProperty('Foo', 'y');
$prop->setAccessible(true); /* As of PHP 5.3.0 */
var_dump($prop->getValue($obj)); // int(2)

$prop = new ReflectionProperty('Foo', 'z');
$prop->setAccessible(true); /* As of PHP 5.3.0 */
var_dump($prop->getValue($obj)); // int(2)

?>

上面的例子会输出类似于:

代码语言:javascript
复制
int(2)
int(3)

← ReflectionProperty::__clone

ReflectionProperty::export →

扫码关注腾讯云开发者

领取腾讯云代金券