From: Christoph M. Becker Date: Thu, 9 Apr 2020 08:55:53 +0000 (+0200) Subject: Fix #79462: method_exists and property_exists incoherent behavior X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e6458d67cfa54cdcd6d3c96897106fa8cb738414;p=php 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. --- diff --git a/NEWS b/NEWS index 1c25db5f08..2d1cc8a15c 100644 --- a/NEWS +++ b/NEWS @@ -16,6 +16,8 @@ PHP NEWS abstract trait function). (Nikita) . Fixed bug #62609 (Allow implementing Traversable on abstract classes). (Nikita) + . Fixed bug #79462 (method_exists and property_exists incoherent behavior). + (cmb) - CURL: . Bumped required libcurl version to 7.29.0. (cmb) diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index 3b0e05c212..4753e9b7c1 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -1107,7 +1107,8 @@ ZEND_FUNCTION(method_exists) RETURN_FALSE; } } else { - RETURN_FALSE; + zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(klass)); + RETURN_THROWS(); } lcname = zend_string_tolower(method_name); diff --git a/ext/standard/tests/class_object/method_exists_variation_001.phpt b/ext/standard/tests/class_object/method_exists_variation_001.phpt index 3ad49456c6..b408b8eb0d 100644 --- a/ext/standard/tests/class_object/method_exists_variation_001.phpt +++ b/ext/standard/tests/class_object/method_exists_variation_001.phpt @@ -78,7 +78,11 @@ $values = array( foreach($values as $value) { echo "\nArg value $value \n"; - var_dump( method_exists($value, $method) ); + try { + var_dump( method_exists($value, $method) ); + } catch (TypeError $e) { + echo $e->getMessage(), PHP_EOL; + } }; echo "Done"; @@ -89,69 +93,69 @@ Error: 2 - Undefined variable $undefined_var Error: 2 - Undefined variable $unset_var Arg value 0 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given Arg value 1 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given Arg value 12345 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given Arg value -2345 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, int given Arg value 10.5 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, float given Arg value -10.5 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, float given Arg value 101234567000 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, float given Arg value 1.07654321E-9 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, float given Arg value 0.5 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, float given Error: 2 - Array to string conversion Arg value Array -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given Error: 2 - Array to string conversion Arg value Array -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given Error: 2 - Array to string conversion Arg value Array -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given Error: 2 - Array to string conversion Arg value Array -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given Error: 2 - Array to string conversion Arg value Array -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given Arg value -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given Arg value -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given Arg value 1 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, bool given Arg value -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, bool given Arg value 1 -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, bool given Arg value -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, bool given Arg value bool(false) @@ -168,8 +172,8 @@ In autoload(String) bool(false) Arg value -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given Arg value -bool(false) +method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given Done