Req #63879 [Opn]: add cast_object (to ARRAY) to SplFixedArray

From: Date: Wed, 02 Jan 2013 10:35:09 +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-175693@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:         cataphract@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:

Wouldn't it be better to fix compare_object() instead (for the specific problem you present)?


Previous Comments:
------------------------------------------------------------------------
[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


Thread (4 messages)

« previous php.bugs (#175693) next »