Req #63879 [Opn]: add cast_object (to ARRAY) to SplFixedArray
| From: | cmb@php.net | Date: | Mon, 08 Aug 2016 11:54:54 +0000 |
| Subject: | Req #63879 [Opn]: add cast_object (to ARRAY) to SplFixedArray | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-203077@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63879&edit=1
ID: 63879
Updated by: cmb@php.net
Reported by: gynvael at coldwind dot pl
Summary: add cast_object (to ARRAY) to SplFixedArray
Status: Open
Type: Feature/Change Request
Package: SPL related
Operating System: any
PHP Version: 5.4.10
Block user comment: N
Private report: N
New Comment:
For reference: <https://3v4l.org/elaM7>.
As of PHP 5.6.0 the proper way to handle this appears to be
overriding compare[1]. That would be more efficient, because the
SplFixedArray wouldn't have to be cast to array at all.
[1] <https://github.com/php/php-src/blob/PHP-5.6.0/Zend/zend_object_handlers.h#L149>
Previous Comments:
------------------------------------------------------------------------
[2013-01-02 14:28:14] gynvael at coldwind dot pl
> Wouldn't it be better to fix compare_object() instead (for the
> specific problem you present)?
This is an ARRAY vs OBJECT comparison, so compare_object is never called (so it
would be hard to fix anything there).
Instead a cast_object is called with ARRAY as the destination type.
Looking at how the PHP objects work internally I would say that adding a
cast_object in this case would be the right way to solve this.
------------------------------------------------------------------------
[2013-01-02 10:35:09] cataphract@php.net
Wouldn't it be better to fix compare_object() instead (for the specific problem you present)?
------------------------------------------------------------------------
[2012-12-31 17:35:43] gynvael at coldwind dot pl
Description:
------------
When comparing (using == operator) an ARRAY with SplFixedArray object one always
gets "false", even if arrays are exact match.
However, casting SplFixedArray to (array) beforehand does a correct (as far as
the == operator goes anyway) comparison.
For convenience it would be good to add a cast_object handler that does the same
thing that SplFixedArray::toArray function does (returns a PHP ARRAY matching
the SplFixedArray objects content).
Details:
The == operator uses cast_object handler when comparing ARRAY with SplFixedArray
object. The SplFixedArray has the default PHP cast_object handler
(zend_std_cast_object_tostring), which fails to convert SplFixedArray to ARRAY.
In such case the comparison is always false (not equal).
Adding a custom cast_object handler to SplFixedArray that does the same thing
that ::toArray function does, would enable a developer to use $splarray ==
$array to perform a comparison, instead of using an explicit cast or calling the
toArray() function explicitly (see example code below).
Test script:
---------------
<?php
$arr = array(1,2,3);
$splarr = SplFixedArray::fromArray($arr);
var_dump($splarr == $arr); // Not Equal (should be)
var_dump((array)$splarr == $arr); // Equal
var_dump($splarr->toArray() == $arr); // Equal
Expected result:
----------------
bool(true)
bool(true)
bool(true)
Actual result:
--------------
bool(false)
bool(true)
bool(true)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63879&edit=1