php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38049 A problem with values returned by ArrayAccess::offsetGet()
Submitted: 2006-07-09 16:43 UTC Modified: 2006-09-22 08:53 UTC
From: vma1 at abv dot bg Assigned: bjori (profile)
Status: Closed Package: Arrays related
PHP Version: 5CVS-2006-07-09 (snap) OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vma1 at abv dot bg
New email:
PHP Version: OS:

 

 [2006-07-09 16:43 UTC] vma1 at abv dot bg
Description:
------------
Values returned by ArrayAccess::offsetGet() cannot be used as a search key in a call to array_key_exists(). It looks like for some reason the scripting engine requires that the first argument of array_key_exists() is writable, which conflicts with the documented prototype of array_key_exists().

The sample code works fine with PHP 5.1.4 so it is a problem introduced in PHP 5.2.

Reproduce code:
---------------
<?php
class array_obj implements ArrayAccess
{
	public function offsetExists ($index) {return (true);}

	public function offsetGet ($index) {return (5);}

	public function offsetSet ($index, $value) {}

	public function offsetUnset ($index) {}
}

$obj = new array_obj ();
array_key_exists ($obj [0], array ("a", "b", "c"));
?>


Expected result:
----------------
The program should terminate with no output at all.

Actual result:
--------------
Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in /archive/error.php on line 14


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-09 19:52 UTC] tony2001@php.net
Yeah, you're right it works fine in 5.1.4, but new arginfo causes this in 5.2.
Hannes, are you sure arginfo for array_key_exists() is correct? I don't see any reasons to use references there, though.

This patch fixes it for me:
Index: ext/standard/basic_functions.c
===================================================================
RCS file: /repository/php-src/ext/standard/basic_functions.c,v
retrieving revision 1.725.2.31.2.15
diff -u -p -d -r1.725.2.31.2.15 basic_functions.c
--- ext/standard/basic_functions.c      2 Jul 2006 00:11:05 -0000       1.725.2.31.2.15
+++ ext/standard/basic_functions.c      9 Jul 2006 19:51:24 -0000
@@ -655,9 +655,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_array_map
 ZEND_END_ARG_INFO()

 static
-ZEND_BEGIN_ARG_INFO(arginfo_array_key_exists, ZEND_SEND_PREFER_REF)
-       ZEND_ARG_INFO(ZEND_SEND_PREFER_REF, key)
-       ZEND_ARG_INFO(ZEND_SEND_PREFER_REF, search)
+ZEND_BEGIN_ARG_INFO(arginfo_array_key_exists, 0)
+       ZEND_ARG_INFO(0, key)
+       ZEND_ARG_INFO(0, search)
 ZEND_END_ARG_INFO()

 static

 [2006-07-12 20:23 UTC] bjori@php.net
Yes, array_key_exists() should _prefer_ references for 
performance reasons (see bug #37563).
For what ever reason the engine however ignores 
the "prefer" part..
 [2006-09-22 08:53 UTC] bjori@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Ilia reverted the patch for bug #37563
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC