php.net |  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
Votes:1
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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gynvael at coldwind dot pl
New email:
PHP Version: OS:

 

 [2012-12-31 17:35 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-02 10:35 UTC] cataphract@php.net
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] cmb@php.net
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>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 09:01:28 2024 UTC