Skip to content

Commit 0b6063f

Browse files
committed
Restore array_key_exists() compatibility for ArrayObject
Doing this by special-casing array_key_exists() for ArrayObject.
1 parent da48671 commit 0b6063f

File tree

6 files changed

+27
-20
lines changed

6 files changed

+27
-20
lines changed

UPGRADING

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ PHP 7.4 UPGRADE NOTES
4040
flag was specified. Other affected operations are:
4141

4242
* ReflectionObject::getProperties()
43-
* array_key_exists(). Use isset() or offsetExists() instead.
4443
* reset(), current(), etc. Use Iterator methods instead.
4544
* Potentially others working on object properties as a list.
4645

Zend/zend_object_handlers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,7 @@ ZEND_API HashTable *zend_std_get_properties_for(zval *obj, zend_prop_purpose pur
17541754
case ZEND_PROP_PURPOSE_SERIALIZE:
17551755
case ZEND_PROP_PURPOSE_VAR_EXPORT:
17561756
case ZEND_PROP_PURPOSE_JSON:
1757+
case _ZEND_PROP_PURPOSE_ARRAY_KEY_EXISTS:
17571758
ht = Z_OBJ_HT_P(obj)->get_properties(obj);
17581759
if (ht && !(GC_FLAGS(ht) & GC_IMMUTABLE)) {
17591760
GC_ADDREF(ht);

Zend/zend_object_handlers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ typedef enum _zend_prop_purpose {
106106
ZEND_PROP_PURPOSE_VAR_EXPORT,
107107
/* Used for json_encode(). */
108108
ZEND_PROP_PURPOSE_JSON,
109+
/* array_key_exists(). Not intended for general use! */
110+
_ZEND_PROP_PURPOSE_ARRAY_KEY_EXISTS,
109111
/* Dummy member to ensure that "default" is specified. */
110112
_ZEND_PROP_PURPOSE_NON_EXHAUSTIVE_ENUM
111113
} zend_prop_purpose;

ext/spl/spl_array.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ static HashTable *spl_array_get_properties_for(zval *object, zend_prop_purpose p
805805
break;
806806
case ZEND_PROP_PURPOSE_VAR_EXPORT:
807807
case ZEND_PROP_PURPOSE_JSON:
808+
case _ZEND_PROP_PURPOSE_ARRAY_KEY_EXISTS:
808809
dup = 0;
809810
break;
810811
default:

ext/spl/tests/bug61347.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var_dump(isset($b['no_exists'])); //false
1212
var_dump(empty($b['b'])); //true
1313
var_dump(empty($b[37])); //true
1414

15-
var_dump(array_key_exists('b', $b)); //false
15+
var_dump(array_key_exists('b', $b)); //true
1616
var_dump($b['b']);
1717

1818
$a = array('b' => '', 37 => false);
@@ -31,7 +31,7 @@ bool(false)
3131
bool(false)
3232
bool(true)
3333
bool(true)
34-
bool(false)
34+
bool(true)
3535
NULL
3636
bool(true)
3737
bool(true)

ext/standard/array.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6247,34 +6247,38 @@ PHP_FUNCTION(array_map)
62476247
Checks if the given key or index exists in the array */
62486248
PHP_FUNCTION(array_key_exists)
62496249
{
6250-
zval *key; /* key to check for */
6251-
HashTable *array; /* array to check in */
6250+
zval *key;
6251+
zval *array;
6252+
HashTable *ht;
62526253

62536254
ZEND_PARSE_PARAMETERS_START(2, 2)
62546255
Z_PARAM_ZVAL(key)
6255-
Z_PARAM_ARRAY_OR_OBJECT_HT(array)
6256+
Z_PARAM_ARRAY_OR_OBJECT(array)
62566257
ZEND_PARSE_PARAMETERS_END();
62576258

6259+
if (EXPECTED(Z_TYPE_P(array) == IS_ARRAY)) {
6260+
ht = Z_ARRVAL_P(array);
6261+
} else {
6262+
ht = zend_get_properties_for(array, ZEND_PROP_PURPOSE_ARRAY_CAST);
6263+
}
6264+
62586265
switch (Z_TYPE_P(key)) {
62596266
case IS_STRING:
6260-
if (zend_symtable_exists_ind(array, Z_STR_P(key))) {
6261-
RETURN_TRUE;
6262-
}
6263-
RETURN_FALSE;
6267+
RETVAL_BOOL(zend_symtable_exists_ind(ht, Z_STR_P(key)));
6268+
break;
62646269
case IS_LONG:
6265-
if (zend_hash_index_exists(array, Z_LVAL_P(key))) {
6266-
RETURN_TRUE;
6267-
}
6268-
RETURN_FALSE;
6270+
RETVAL_BOOL(zend_hash_index_exists(ht, Z_LVAL_P(key)));
6271+
break;
62696272
case IS_NULL:
6270-
if (zend_hash_exists_ind(array, ZSTR_EMPTY_ALLOC())) {
6271-
RETURN_TRUE;
6272-
}
6273-
RETURN_FALSE;
6274-
6273+
RETVAL_BOOL(zend_hash_exists_ind(ht, ZSTR_EMPTY_ALLOC()));
6274+
break;
62756275
default:
62766276
php_error_docref(NULL, E_WARNING, "The first argument should be either a string or an integer");
6277-
RETURN_FALSE;
6277+
RETVAL_FALSE;
6278+
}
6279+
6280+
if (Z_TYPE_P(array) != IS_ARRAY) {
6281+
zend_release_properties(ht);
62786282
}
62796283
}
62806284
/* }}} */

0 commit comments

Comments
 (0)