Skip to content

Commit

Permalink
Deprecate array_key_exists() on objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 22, 2019
1 parent b2ea507 commit 0ba7c3e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -2586,6 +2586,10 @@ static zend_never_inline uint32_t ZEND_FASTCALL zend_array_key_exists_fast(HashT
static zend_never_inline uint32_t ZEND_FASTCALL zend_array_key_exists_slow(zval *subject, zval *key OPLINE_DC EXECUTE_DATA_DC)
{
if (EXPECTED(Z_TYPE_P(subject) == IS_OBJECT)) {
zend_error(E_DEPRECATED, "array_key_exists(): "
"Using array_key_exists() on objects is deprecated. "
"Use isset() or property_exists() instead");

HashTable *ht = zend_get_properties_for(subject, ZEND_PROP_PURPOSE_ARRAY_CAST);
uint32_t result = zend_array_key_exists_fast(ht, key OPLINE_CC EXECUTE_DATA_CC);
zend_release_properties(ht);
Expand Down
4 changes: 3 additions & 1 deletion ext/spl/tests/bug61347.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ var_dump(isset($b[37])); //true
var_dump(isset($b['no_exists'])); //false
var_dump(empty($b['b'])); //true
var_dump(empty($b[37])); //true
--EXPECT--
--EXPECTF--
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(true)
NULL
bool(true)
Expand Down
3 changes: 3 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6330,6 +6330,9 @@ PHP_FUNCTION(array_key_exists)
ht = Z_ARRVAL_P(array);
} else {
ht = zend_get_properties_for(array, ZEND_PROP_PURPOSE_ARRAY_CAST);
php_error_docref(NULL, E_DEPRECATED,
"Using array_key_exists() on objects is deprecated. "
"Use isset() or property_exists() instead");
}

switch (Z_TYPE_P(key)) {
Expand Down
12 changes: 12 additions & 0 deletions ext/standard/tests/array/array_key_exists.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,25 @@ Warning: array_key_exists(): The first argument should be either a string or an
bool(false)

*** Testing operation on objects ***

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(false)

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(false)

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(true)

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(false)

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(true)
bool(true)

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d

Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
bool(false)
Done
8 changes: 7 additions & 1 deletion ext/standard/tests/array/array_key_exists_object1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ var_dump($class2);

echo "Done";
?>
--EXPECT--
--EXPECTF--
*** Testing array_key_exists() : object functionality ***

-- Do not assign a value to $class1->var3 --
$key = var1:

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(true)
$key = var3:

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(true)
$class1:
object(myClass)#1 (3) {
Expand All @@ -66,6 +70,8 @@ object(myClass)#1 (3) {

-- Assign a value to $class2->var3 --
$key = var3:

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(true)
$class2:
object(myClass)#2 (3) {
Expand Down
10 changes: 9 additions & 1 deletion ext/standard/tests/array/array_key_exists_object2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,21 @@ var_dump($class2);

echo "Done";
?>
--EXPECT--
--EXPECTF--
*** Testing array_key_exists() : object functionality ***

-- Do not assign a value to $class1->var3 --
$key = var1:

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(true)
$key = var2:

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(false)
$key = var3:

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(false)
$class1:
object(myClass)#1 (3) {
Expand All @@ -70,6 +76,8 @@ object(myClass)#1 (3) {

-- Assign a value to $class2->var3 --
$key = var3:

Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in %s on line %d
bool(false)
$class2:
object(myClass)#2 (3) {
Expand Down

0 comments on commit 0ba7c3e

Please sign in to comment.