xref: /PHP-8.0/Zend/tests/011.phpt (revision 55a3e5b9)
1--TEST--
2property_exists() tests
3--FILE--
4<?php
5
6class foo {
7    public $pp1 = 1;
8    private $pp2 = 2;
9    protected $pp3 = 3;
10
11    function bar() {
12        var_dump(property_exists("foo","pp1"));
13        var_dump(property_exists("foo","pp2"));
14        var_dump(property_exists("foo","pp3"));
15    }
16}
17
18class bar extends foo {
19    function test() {
20        var_dump(property_exists("foo","pp1"));
21        var_dump(property_exists("foo","pp2"));
22        var_dump(property_exists("foo","pp3"));
23    }
24}
25
26var_dump(property_exists("foo","pp1"));
27var_dump(property_exists("foo","pp2"));
28var_dump(property_exists("foo","pp3"));
29var_dump(property_exists("foo","nonexistent"));
30var_dump(property_exists("fo","nonexistent"));
31var_dump(property_exists("foo",""));
32var_dump(property_exists("","test"));
33var_dump(property_exists("",""));
34
35$foo = new foo;
36
37var_dump(property_exists($foo,"pp1"));
38var_dump(property_exists($foo,"pp2"));
39var_dump(property_exists($foo,"pp3"));
40var_dump(property_exists($foo,"nonexistent"));
41var_dump(property_exists($foo,""));
42
43try {
44    var_dump(property_exists(array(), "test"));
45} catch (\TypeError $e) {
46    echo $e->getMessage() . \PHP_EOL;
47}
48try {
49    var_dump(property_exists(1, "test"));
50} catch (\TypeError $e) {
51    echo $e->getMessage() . \PHP_EOL;
52}
53try {
54    var_dump(property_exists(3.14, "test"));
55} catch (\TypeError $e) {
56    echo $e->getMessage() . \PHP_EOL;
57}
58try {
59    var_dump(property_exists(true, "test"));
60} catch (\TypeError $e) {
61    echo $e->getMessage() . \PHP_EOL;
62}
63try {
64    var_dump(property_exists(null, "test"));
65} catch (\TypeError $e) {
66    echo $e->getMessage() . \PHP_EOL;
67}
68
69$foo->bar();
70
71$bar = new bar;
72$bar->test();
73
74?>
75--EXPECT--
76bool(true)
77bool(true)
78bool(true)
79bool(false)
80bool(false)
81bool(false)
82bool(false)
83bool(false)
84bool(true)
85bool(true)
86bool(true)
87bool(false)
88bool(false)
89property_exists(): Argument #1 ($object_or_class) must be of type object|string, array given
90property_exists(): Argument #1 ($object_or_class) must be of type object|string, int given
91property_exists(): Argument #1 ($object_or_class) must be of type object|string, float given
92property_exists(): Argument #1 ($object_or_class) must be of type object|string, bool given
93property_exists(): Argument #1 ($object_or_class) must be of type object|string, null given
94bool(true)
95bool(true)
96bool(true)
97bool(true)
98bool(true)
99bool(true)
100