Edit report at https://bugs.php.net/bug.php?id=63879&edit=1
ID: 63879 User updated by: gynvael at coldwind dot pl 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)? 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. Previous Comments: ------------------------------------------------------------------------ [2013-01-02 10:35:09] [email protected] 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
