com php-src: Restore array_key_exists() compatibility for ArrayObject: UPGRADING Zend/zend_object_hand lers.c Zend/zend_object_handlers.h
ext/spl/spl_ar ray.c ext/spl/tests/bug61347.phpt ext/standar d/array.c

From: Date: Thu, 11 Oct 2018 14:10:22 +0000
Subject: com php-src: Restore array_key_exists() compatibility for ArrayObject: UPGRADING Zend/zend_object_hand lers.c Zend/zend_object_handlers.h
ext/spl/spl_ar ray.c ext/spl/tests/bug61347.phpt ext/standar d/array.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-103441@lists.php.net to get a copy of this message
Commit: 0b6063f33112b4685bc3c23ae823556fec8f32f1 Author: Nikita Popov <nikita.ppv@gmail.com> Thu, 11 Oct 2018 16:08:58 +0200 Parents: da4867195965c2b6e9a04037e1edd40967463db2 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=0b6063f33112b4685bc3c23ae823556fec8f32f1 Log: Restore array_key_exists() compatibility for ArrayObject Doing this by special-casing array_key_exists() for ArrayObject. Changed paths: M UPGRADING M Zend/zend_object_handlers.c M Zend/zend_object_handlers.h M ext/spl/spl_array.c M ext/spl/tests/bug61347.phpt M ext/standard/array.c Diff: diff --git a/UPGRADING b/UPGRADING index cdec63d..98ceff4 100644 --- a/UPGRADING +++ b/UPGRADING @@ -40,7 +40,6 @@ PHP 7.4 UPGRADE NOTES flag was specified. Other affected operations are: * ReflectionObject::getProperties() - * array_key_exists(). Use isset() or offsetExists() instead. * reset(), current(), etc. Use Iterator methods instead. * Potentially others working on object properties as a list. diff --git a/Zend/zend_object_handlers.c b/Zend/zend_object_handlers.c index ccc4ebe..21e47bc 100644 --- a/Zend/zend_object_handlers.c +++ b/Zend/zend_object_handlers.c @@ -1754,6 +1754,7 @@ ZEND_API HashTable *zend_std_get_properties_for(zval *obj, zend_prop_purpose pur case ZEND_PROP_PURPOSE_SERIALIZE: case ZEND_PROP_PURPOSE_VAR_EXPORT: case ZEND_PROP_PURPOSE_JSON: + case _ZEND_PROP_PURPOSE_ARRAY_KEY_EXISTS: ht = Z_OBJ_HT_P(obj)->get_properties(obj); if (ht && !(GC_FLAGS(ht) & GC_IMMUTABLE)) { GC_ADDREF(ht); diff --git a/Zend/zend_object_handlers.h b/Zend/zend_object_handlers.h index 3d89783..83a22c4 100644 --- a/Zend/zend_object_handlers.h +++ b/Zend/zend_object_handlers.h @@ -106,6 +106,8 @@ typedef enum _zend_prop_purpose { ZEND_PROP_PURPOSE_VAR_EXPORT, /* Used for json_encode(). */ ZEND_PROP_PURPOSE_JSON, + /* array_key_exists(). Not intended for general use! */ + _ZEND_PROP_PURPOSE_ARRAY_KEY_EXISTS, /* Dummy member to ensure that "default" is specified. */ _ZEND_PROP_PURPOSE_NON_EXHAUSTIVE_ENUM } zend_prop_purpose; diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index 9d7a51a..04e73bf 100644 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -805,6 +805,7 @@ static HashTable *spl_array_get_properties_for(zval *object, zend_prop_purpose p break; case ZEND_PROP_PURPOSE_VAR_EXPORT: case ZEND_PROP_PURPOSE_JSON: + case _ZEND_PROP_PURPOSE_ARRAY_KEY_EXISTS: dup = 0; break; default: diff --git a/ext/spl/tests/bug61347.phpt b/ext/spl/tests/bug61347.phpt index 720cf5b..cb09185 100644 --- a/ext/spl/tests/bug61347.phpt +++ b/ext/spl/tests/bug61347.phpt @@ -12,7 +12,7 @@ var_dump(isset($b['no_exists'])); //false var_dump(empty($b['b'])); //true var_dump(empty($b[37])); //true -var_dump(array_key_exists('b', $b)); //false +var_dump(array_key_exists('b', $b)); //true var_dump($b['b']); $a = array('b' => '', 37 => false); @@ -31,7 +31,7 @@ bool(false) bool(false) bool(true) bool(true) -bool(false) +bool(true) NULL bool(true) bool(true) diff --git a/ext/standard/array.c b/ext/standard/array.c index 8ebf4e1..4d94c54 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -6247,34 +6247,38 @@ PHP_FUNCTION(array_map) Checks if the given key or index exists in the array */ PHP_FUNCTION(array_key_exists) { - zval *key; /* key to check for */ - HashTable *array; /* array to check in */ + zval *key; + zval *array; + HashTable *ht; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_ZVAL(key) - Z_PARAM_ARRAY_OR_OBJECT_HT(array) + Z_PARAM_ARRAY_OR_OBJECT(array) ZEND_PARSE_PARAMETERS_END(); + if (EXPECTED(Z_TYPE_P(array) == IS_ARRAY)) { + ht = Z_ARRVAL_P(array); + } else { + ht = zend_get_properties_for(array, ZEND_PROP_PURPOSE_ARRAY_CAST); + } + switch (Z_TYPE_P(key)) { case IS_STRING: - if (zend_symtable_exists_ind(array, Z_STR_P(key))) { - RETURN_TRUE; - } - RETURN_FALSE; + RETVAL_BOOL(zend_symtable_exists_ind(ht, Z_STR_P(key))); + break; case IS_LONG: - if (zend_hash_index_exists(array, Z_LVAL_P(key))) { - RETURN_TRUE; - } - RETURN_FALSE; + RETVAL_BOOL(zend_hash_index_exists(ht, Z_LVAL_P(key))); + break; case IS_NULL: - if (zend_hash_exists_ind(array, ZSTR_EMPTY_ALLOC())) { - RETURN_TRUE; - } - RETURN_FALSE; - + RETVAL_BOOL(zend_hash_exists_ind(ht, ZSTR_EMPTY_ALLOC())); + break; default: php_error_docref(NULL, E_WARNING, "The first argument should be either a string or an integer"); - RETURN_FALSE; + RETVAL_FALSE; + } + + if (Z_TYPE_P(array) != IS_ARRAY) { + zend_release_properties(ht); } } /* }}} */

« previous php.cvs (#103441) next »