Re: object literals

From: Date: Fri, 01 May 2009 09:07:25 +0000
Subject: Re: object literals
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-292174@lists.php.net to get a copy of this message
Richard Heyes wrote: > Hi, > >> $x = (object) array('a'=>1, 'b'=>3, ...); >> >> which works but isn't very lovely. it's neater in, for example, javascript. > > Well, you could wrap it up in a function to make it a bit lovelier. Eg: > > $foo = createObject(array('key' => 'value')); > > It's not great, but PHP doesn't have a object literal syntax AFAIK. > You could use JSON, $foo = json_decode('{"a":1,"b":3}'); but I guess that's not much better than Richard's suggestion. -- Peter Ford phone: 01580 893333 Developer fax: 01580 893399 Justcroft International Ltd., Staplehurst, Kent

« previous php.general (#292174) next »