update : 2015.11.03
php.shukuma.com

검색:
 
 
Construct a ReflectionProperty object

ReflectionProperty::__construct

(PHP 5)

ReflectionProperty::__constructConstruct a ReflectionProperty object

설명

public ReflectionProperty::__construct ( mixed $class , string $name )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

class

The class name, that contains the property.

name

The name of the property being reflected.

반환값

값을 반환하지 않습니다.

오류/예외

Trying to get or set private or protected class property's values will result in an exception being thrown.

예제

Example #1 ReflectionProperty::__construct() example

<?php
class String
{
    public 
$length  5;
}

// Create an instance of the ReflectionProperty class
$prop = new ReflectionProperty('String''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 String
$obj= new String();

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

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

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

위 예제의 출력 예시:

===> 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(String)#2 (1) {
  ["length"]=>
  int(10)
}

Example #2 Getting value from private and protected properties using ReflectionProperty class

<?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)

?>

위 예제의 출력 예시:

int(2)
int(3)

참고