exactly, why bother with JavaScript literal representation? the foreach loop was just an alternative to explicit object cast cause you did not like it .... I though it was more about control over what you want to cast and what you do not ... in any case, what I said does not change cause in php you have natural type hint over arrays and these are normally slightly faster so I do not get why you want objects rather than arrays ... > Subject: RE: object literals > From: robert@xxxxxxxxxxxxx > To: an_red@xxxxxxxxxxx > CC: fsb@xxxxxxxxxx; php-general@xxxxxxxxxxxxx > Date: Fri, 1 May 2009 12:14:21 -0400 > > On Fri, 2009-05-01 at 17:52 +0200, Andrea Giammarchi wrote: > > you are in PHP, not in JavaScript. > > > > In PHP arrays are like collections or hash tables. > > if you strictly need object cause > > > > $o->stuff > > is better than > > $o['stuff'] > > > > having exactly the same number of characters, you can create a > > function like > > > > function o(array $a){ > > $o = new stdClass; > > foreach($a as $key => $value) > > $o->$key = $value; > > return $o; > > } > > > > > > and the syntax will be > > > > $o = o(array( > > 'a' => "b", > > 'c' => "d" > > )); > > > > spot the difference from (object) array(whatever) ? > > > > I do not ... and that's why json_encode resolves associative arrays > > rather than list automatically but still, if you are in PHP, you > > should think about being familiar with associative arrays, also > > because so far is the only class you cannot create/extend. > > > > class string { > > // ok > > } > > > > class object { > > // ok > > } > > > > class array { > > // no way > > } > > > > Regards > > First off, you compared the syntax between creating a PHP array and a > JavaScript object when the previous post specifically spoke about > getting a PHP "OBJECT". Now you've made a rather lengthy and redundant > post trying to describe to me objects versus arrays in PHP. Lastly > you've suggested writing a function to convert an array to an object > using a foreach loop for the members which is completely unnecessary. > The following will suffice: > > <?php > > function o( array $a ) > { > return (object)$a; > } > > ?> > > ... and the syntax will be: > > <?php > > $o = o(array > ( > 'a' => "b", > 'c' => "d", > )); > > ?> > > But why bother when you could have just done: > > <?php > > $o = (object)array > ( > 'a' => "b", > 'c' => "d", > ); > > ?> > > Cheers, > Rob. > -- > http://www.interjinn.com > Application and Templating Framework for PHP > _________________________________________________________________ Windows Live™: Keep your life in sync. Check it out! http://windowslive.com/explore?ocid=TXT_TAGLM_WL_t1_allup_explore_012009