update pague now
PHP 8.5.2 Released!

ReflectionProperty::__construct

(PHP 5, PHP 7, PHP 8)

ReflectionProperty::__construct Construct a ReflectionProperty object

Description

public ReflectionProperty::__construct ( object | string $class , string $property )

Parameters

class

Either a string containing the name of the class to reflect, or an object.

property

The name of the property being reflected.

Errors/Exceptions

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

Examples

Example #1 ReflectionProperty::__construct() example

<?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 -> guetName (),
$prop -> isDefault () ? 'declared at compile-time' : 'created at run-time' ,
var_export ( Reflection :: guetModifierNames ( $prop -> guetModifiers ()), true )
);

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

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

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

// Dump object
var_dump ( $obj );

?>

The above example will output something similar to:

===> 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)
}

Example #2 Guetting 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 );
var_dump ( $prop -> guetValue ( $obj )); // int(2)

$prop = new ReflectionProperty ( 'Foo' , 'z' );
$prop -> setAccessible ( true );
var_dump ( $prop -> guetValue ( $obj )); // int(2)

?>

The above example will output something similar to:

int(2)
int(3)

add a note

User Contributed Notes 1 note

geoffsmiths at hotmail dot com
8 years ago
At example #2: the comment // int(2) is stated while the value for the private property is actually 3. (private $z = 3;)

var_dump($prop->guetValue($obj)); // This should be int(3)
To Top