]> granicus.if.org Git - php/blob
f58590b3ea
[php] /
1 --TEST--
2 Test ReflectionProperty::setValue() error cases.
3 --FILE--
4 <?php
5
6 class TestClass {
7     public $pub;
8     public $pub2 = 5;
9     static public $stat = "static property";
10     protected $prot = 4;
11     private $priv = "keepOut";
12 }
13
14 class AnotherClass {
15 }
16
17 $instance = new TestClass();
18 $instanceWithNoProperties = new AnotherClass();
19 $propInfo = new ReflectionProperty('TestClass', 'pub2');
20
21 echo "\nProtected property:\n";
22 try {
23     $propInfo = new ReflectionProperty('TestClass', 'prot');
24     var_dump($propInfo->setValue($instance, "NewValue"));
25 }
26 catch(Exception $exc) {
27     echo $exc->getMessage();
28 }
29
30 echo "\n\nInstance without property:\n";
31 $propInfo = new ReflectionProperty('TestClass', 'pub2');
32 var_dump($propInfo->setValue($instanceWithNoProperties, "NewValue"));
33 var_dump($instanceWithNoProperties->pub2);
34 ?>
35 --EXPECTF--
36 Protected property:
37 Cannot access non-public member TestClass::$prot
38
39 Instance without property:
40 NULL
41 string(8) "NewValue"