Doc #63217 [Opn]: Behavior of implementing the ArrayAccess interface has been changed in php 5.4

From: Date: Tue, 24 Nov 2015 15:10:50 +0000
Subject: Doc #63217 [Opn]: Behavior of implementing the ArrayAccess interface has been changed in php 5.4
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-12882@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63217&edit=1

 ID:                 63217
 Updated by:         levim@php.net
 Reported by:        kmsheng at pixnet dot tw
 Summary:            Behavior of implementing the ArrayAccess interface
                     has been changed in php 5.4
 Status:             Open
 Type:               Documentation Problem
 Package:            Arrays related
 Operating System:   freebsd
 PHP Version:        5.4Git-2012-10-04 (Git)
-Assigned To:        
+Assigned To:        ajf
 Block user comment: N
 Private report:     N

 New Comment:

Changing this back to a bug status. Here is an example where I hope it is clear it is a bug (on
3v4l: https://3v4l.org/XGtVh):

<?php
class Dictionary implements ArrayAccess {
	function offsetExists($offset) {}
	function offsetGet($offset) {}
	function offsetUnset($offset) {}

	function offsetSet($offset, $value) {
		if (!is_string($offset)) {
			throw new InvalidArgumentException();
		}
	}
}

try {
    $Dictionary = new Dictionary();
    $Dictionary["12"] = 0xDEADBEEF;
    echo "No Exception for \"12\"\n";
} catch (InvalidArgumentException $e) {
    echo "Caught Exception for \"12\"\n";
}

try {
    $str = "12";
    $Dictionary[$str] = 0xDEADBEEF;
    echo "No Exception for \$variable = \"12\"\n";
} catch (InvalidArgumentException $e) {
    echo "Caught Exception for \$variable = \"12\"\n";
}

?>


Previous Comments:
------------------------------------------------------------------------
[2015-11-24 05:22:51] rudolf dot theunissen at gmail dot com

It makes sense for array keys to be coerced to intergers, ie. 1, 1.0, and "1" will attempt
to access the same index. However, this does not make sense for ArrayAccess. The argument for
consistency is poor, because float and object keys are handled differently already (floats stay
floats and objects don't raise warnings).  One example use case is SplObjectStorage, which
makes use of object keys via ArrayAccess.

With the introduction of strict types and scalar typehints, it makes sense for the implementation to
be responsible for handling various key types.

ArrayAccess should only provide array syntax, not array behaviour.

Test script:
https://3v4l.org/0BMYh
---------------
<?php

class Test implements ArrayAccess
{
    public function offsetGet($offset){
        return gettype($offset);
    }
    
    public function offsetSet($offset, $value){}
    public function offsetUnset($offset){}
    public function offsetExists($offset){}
}

$a = array('a', 'b', 'c');
$o = new Test();
$s = '1';

echo $o[1],   "\n"; // 'integer'
echo $o['1'], "\n"; // 'integer' !?
echo $o[1.0], "\n"; // 'double'
echo $o[$s],  "\n"; // 'string'
echo $o[$o],  "\n"; // 'object'

echo $a[1],   "\n"; // 'integer'
echo $a['1'], "\n"; // 'integer'
echo $a[1.0], "\n"; // 'integer'
echo $a[$s],  "\n"; // 'integer'
echo $a[$o],  "\n"; // Warning: Illegal offset type

Output:
---------------
integer
integer
double
string
object
b
b
b
b

Warning: Illegal offset type in...

------------------------------------------------------------------------
[2013-04-07 17:44:34] david at grudl dot com

This is really buggy.

$test['0']; // prints int(0)

but

$key = '0'; 
$test[$key]; // prints string(1) "0"

------------------------------------------------------------------------
[2012-10-05 04:54:32] laruence@php.net

numeric string key will cast to number, I think this is a improvement, to be 
consistent with the internal dim fetch.

but yeah, we need a document of such change, change to doc problem.

------------------------------------------------------------------------
[2012-10-04 10:14:49] kmsheng at pixnet dot tw

Description:
------------
Execute the test script provided below in php 5.3.10 and php 5.4.0-3 may get different results which
are string(1) "0" and int(0).

I don't know if this is a bug or a patch.

Test script:
---------------
<?php
class Test implements ArrayAccess {

    public function offsetExists($off)
    {
    }

    public function offsetUnset($off)
    {
    }

    public function offsetSet($off, $el)
    {
    }

    public function offsetGet($off)
    {
        var_dump($off);
    }
}

$test = new Test();

$test['0'];

Expected result:
----------------
string(1) "0"

Actual result:
--------------
int(0)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=63217&edit=1


Thread (4 messages)

« previous php.doc.bugs (#12882) next »