Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:10089 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 96214 invoked by uid 1010); 25 May 2004 14:31:09 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 95161 invoked by uid 1007); 25 May 2004 14:30:59 -0000 Message-ID: <20040525143059.95129.qmail@pb1.pair.com> To: internals@lists.php.net References: <20040522064028.3397.qmail@pb1.pair.com> <5.1.0.14.2.20040525161238.061c8f78@127.0.0.1> Date: Wed, 26 May 2004 00:29:37 +1000 Lines: 140 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 X-Posted-By: 144.132.76.67 Subject: Re: [PHP-DEV] Re: Internal casting of objects as array keys From: aidan@php.net ("Aidan Lister") Hi Andi, Okay, it's not as simple as I thought it was - and you're correct, the behaviour has not changed for php4/5. Objects in both PHP4 and PHP5 will throw an error if you use the method $blah[$someobject] = 'val'; (first method) Warning: Illegal offset type However, both do not throw an error if you use $blah = array($someobject => 'val'); (second method) When we compare this behaviour with resources, it gets interesting. A resource is casted to an interger when used as an array key for the first method, but not the second. This raises a number of questions: 1) Should the behaviour of method 1, and method 2 be the same. 2) Should the behaviour of objects be the same as resources (in the context of casting for use as an array key) Below is a sample script which shows the differences --------------------
_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] = 'sdf';
$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);
?>
-------------------- The output: ----------- Warning: Illegal offset type on line 9 objects: first method: array(0) { } objects: second method: array(0) { } resources: first method: array(1) { [2]=> string(3) "fp1" } resources: second method: array(0) { } -------------- Thanks. "Andi Gutmans" wrote in message news:5.1.0.14.2.20040525161238.061c8f78@127.0.0.1... > This code doesn't seem to have changed. Can you send me a 3-4 liner which > errors out in PHP 4 and not in PHP 4? > > Thanks, > > Andi > > At 11:03 PM 5/25/2004 +1000, Aidan Lister wrote: > >I'm going to go ahead and post this as a bug, unless there is a reason noone > >replied... > > > >"Aidan Lister" wrote in message > >news:20040522064028.3397.qmail@pb1.pair.com... > > > Hello devs, > > > > > > I'd like to discuss the use of objects as array keys. > > > > > > > > class idobject { > > > private $_id; > > > function __construct ($id) { $this->_id = $id; } > > > function __tostring () { return (int)$this->_id; } > > > } > > > > > > $blah1 = new idobject(1); > > > $blah2 = new idobject(2); > > > $blah3 = new idobject(3); > > > $blah4 = new idobject(4); > > > > > > $array = array ( > > > $blah1 => 'blah1', > > > $blah2 => 'blah2', > > > $blah3 => 'blah3', > > > $blah4 => 'blah4', > > > ); > > > > > > echo "
";
> > > var_dump($array);
> > > echo "
"; > > > ?> > > > > > > In PHP4 this results in an error, > > > In PHP5 there is no error, the array is simply empty. > > > > > > An array key can only be a string/int, thus, when someone attempts to use > >an > > > array as a key would it not make sense to cast the object? > > > > > > This behaviour would then be consistant with resources. > > > > > > Regards, > > > Aidan > > > >-- > >PHP Internals - PHP Runtime Development Mailing List > >To unsubscribe, visit: http://www.php.net/unsub.php