PHP Portal » PHP Handbuch » ReflectionProperty::__construct

Werbung

ReflectionProperty::__construct


(PHP 5)

ReflectionProperty::__constructConstruct a ReflectionProperty object

Beschreibung

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

Warnung

Diese Funktion ist bis jetzt nicht dokumentiert. Es steht nur die Liste der Argumente zur Verfügung.

Parameter-Liste

class

The class name, that contains the property.

name

The name of the property being reflected.

Rückgabewerte

Es wird kein Wert zurückgegeben.

Fehler/Exceptions

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

Beispiele

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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($obj, 10); printf("---> Setting value to 10, new value is: "); var_dump($prop->getValue($obj)); // Dump object var_dump($obj);

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

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

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
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)

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

int(2)
int(3)

Siehe auch