|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63879 add cast_object (to ARRAY) to SplFixedArray
Submitted: 2012-12-31 17:35 UTC Modified: 2016-08-08 11:54 UTC
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: gynvael at coldwind dot pl Assigned:
Status: Open Package: SPL related
PHP Version: 5.4.10 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: gynvael at coldwind dot pl
New email:
PHP Version: OS:


 [2012-12-31 17:35 UTC] gynvael at coldwind dot pl
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).

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:
$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:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-02 10:35 UTC]
Wouldn't it be better to fix compare_object() instead (for the specific problem you present)?
 [2013-01-02 14:28 UTC] 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.
 [2016-08-08 11:54 UTC]
For reference: <>.

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] <>
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 27 07:01:30 2024 UTC