Skip to content

Commit e6458d6

Browse files
committed
Fix #79462: method_exists and property_exists incoherent behavior
Both functions are closely related, so should behave the same for wrong input types, i.e. both should throw a TypeError.
1 parent 1232436 commit e6458d6

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ PHP NEWS
1616
abstract trait function). (Nikita)
1717
. Fixed bug #62609 (Allow implementing Traversable on abstract classes).
1818
(Nikita)
19+
. Fixed bug #79462 (method_exists and property_exists incoherent behavior).
20+
(cmb)
1921

2022
- CURL:
2123
. Bumped required libcurl version to 7.29.0. (cmb)

Zend/zend_builtin_functions.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,8 @@ ZEND_FUNCTION(method_exists)
11071107
RETURN_FALSE;
11081108
}
11091109
} else {
1110-
RETURN_FALSE;
1110+
zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(klass));
1111+
RETURN_THROWS();
11111112
}
11121113

11131114
lcname = zend_string_tolower(method_name);

ext/standard/tests/class_object/method_exists_variation_001.phpt

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ $values = array(
7878

7979
foreach($values as $value) {
8080
echo "\nArg value $value \n";
81-
var_dump( method_exists($value, $method) );
81+
try {
82+
var_dump( method_exists($value, $method) );
83+
} catch (TypeError $e) {
84+
echo $e->getMessage(), PHP_EOL;
85+
}
8286
};
8387

8488
echo "Done";
@@ -89,69 +93,69 @@ Error: 2 - Undefined variable $undefined_var
8993
Error: 2 - Undefined variable $unset_var
9094

9195
Arg value 0
92-
bool(false)
96+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given
9397

9498
Arg value 1
95-
bool(false)
99+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given
96100

97101
Arg value 12345
98-
bool(false)
102+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given
99103

100104
Arg value -2345
101-
bool(false)
105+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given
102106

103107
Arg value 10.5
104-
bool(false)
108+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, float given
105109

106110
Arg value -10.5
107-
bool(false)
111+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, float given
108112

109113
Arg value 101234567000
110-
bool(false)
114+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, float given
111115

112116
Arg value 1.07654321E-9
113-
bool(false)
117+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, float given
114118

115119
Arg value 0.5
116-
bool(false)
120+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, float given
117121
Error: 2 - Array to string conversion
118122

119123
Arg value Array
120-
bool(false)
124+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given
121125
Error: 2 - Array to string conversion
122126

123127
Arg value Array
124-
bool(false)
128+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given
125129
Error: 2 - Array to string conversion
126130

127131
Arg value Array
128-
bool(false)
132+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given
129133
Error: 2 - Array to string conversion
130134

131135
Arg value Array
132-
bool(false)
136+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given
133137
Error: 2 - Array to string conversion
134138

135139
Arg value Array
136-
bool(false)
140+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given
137141

138142
Arg value
139-
bool(false)
143+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given
140144

141145
Arg value
142-
bool(false)
146+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given
143147

144148
Arg value 1
145-
bool(false)
149+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, bool given
146150

147151
Arg value
148-
bool(false)
152+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, bool given
149153

150154
Arg value 1
151-
bool(false)
155+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, bool given
152156

153157
Arg value
154-
bool(false)
158+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, bool given
155159

156160
Arg value
157161
bool(false)
@@ -168,8 +172,8 @@ In autoload(String)
168172
bool(false)
169173

170174
Arg value
171-
bool(false)
175+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given
172176

173177
Arg value
174-
bool(false)
178+
method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given
175179
Done

0 commit comments

Comments
 (0)