From: [EMAIL PROTECTED] Operating system: WinXP PHP version: 5.0.0RC3 PHP Bug Type: Arrays related Bug description: Behavioural issues: $foo = array() vs $foo[] and objects vs resources
Description: ------------ This bug report pertains to two seperate but related problems: 1. Unexpected and undocumented behaviour, between $foo[] and $foo = array() 2. Automatic conversion of objects to strings when used as the key in an array. Part 1; ------- With a given resource, the automatic casting of a resource to an interger differs based on the method of array creation. This is both unexpected, and undocumented behaviour. I can see no reason why this would be a feature, rather than a bug. I suggest the behaviour be changed to be consistant with both methods of array creation. Part 2; ------ When an object is used as the key of an array (with behaviour changing between method of array creation), should n't the object be casted to an interger or the __toString method called? When used with array() no error is thrown, the array is simply empty. With $foo[__object__] an error is thrown. I suggest the behaviour of objects as array keys be changed to be consistant with that of resources. The code in the reproduce code section demonstrates both aspects of the behavioural issue. Reproduce code: --------------- <?php // Create a sample object class id_obj { var $_id; function id_obj($id) { $this->_id = $id; } } // Create two new objects $id1 = new id_obj(1); $id2 = new id_obj(2); // Add them to an array with two different methods $array = array(); $array[$id1] = 'foo'; $array2 = array ($id1 => 'id1', $id2 => 'id2'); echo "objects: first method:\n"; var_dump($array); echo "objects: second method:\n"; var_dump($array2); // Create two resources $fp1 = fsockopen("www.example.com", 80, $errno, $errstr, 30); $fp2 = fsockopen("www.example.com", 80, $errno, $errstr, 30); // Add them to an array with two different methods $resarray = array(); $resarray[$fp1] = 'fp1'; $resarray2 = array($fp1 => 'fp1', $fp2 => 'fp2'); // Show the result echo "resources: first method:\n"; var_dump($resarray); echo "resources: second method:\n"; var_dump($resarray2); ?> -- Edit bug report at http://bugs.php.net/?id=28879&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=28879&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=28879&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=28879&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=28879&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=28879&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=28879&r=needscript Try newer version: http://bugs.php.net/fix.php?id=28879&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=28879&r=support Expected behavior: http://bugs.php.net/fix.php?id=28879&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=28879&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=28879&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=28879&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=28879&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=28879&r=dst IIS Stability: http://bugs.php.net/fix.php?id=28879&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=28879&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=28879&r=float