summaryrefslogtreecommitdiff
path: root/ext/reflection/tests/reflectionProperty_setAccessible.phpt
blob: 5e26b2808aad0bf36cc9f11c707b55c1afd4c0a6 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
--TEST--
Test ReflectionProperty::setAccessible().
--SKIPIF--
<?php extension_loaded('reflection') or die('skip'); ?>
--FILE--
<?php

class TestClass {
    public $pub;
    public $pub2 = 5;
    static public $stat = "static property";
    protected $prot = 4;
    private $priv = "keepOut";
}

class AnotherClass {
}

$instance = new TestClass();

echo "\nProtected property:\n";
$propInfo = new ReflectionProperty('TestClass', 'prot');
try {
    var_dump($propInfo->getValue($instance));
}
catch(Exception $exc) {
    echo $exc->getMessage(), "\n";
}

$propInfo->setAccessible(true);
var_dump($propInfo->getValue($instance));

$propInfo->setAccessible(false);
try {
    var_dump($propInfo->getValue($instance));
}
catch(Exception $exc) {
    echo $exc->getMessage(), "\n";
}
?>
--EXPECTF--

Protected property:
Cannot access non-public member TestClass::prot
int(4)
Cannot access non-public member TestClass::prot