Re: [PHP] object literals
i'm really lazy about typing. I think every good programmer is... ;-) -- Richard Heyes HTML5 graphing: RGraph (www.rgraph.net) PHP mail: RMail (www.phpguru.org/rmail) PHP datagrid: RGrid (www.phpguru.org/rgrid) PHP Template: RTemplate (www.phpguru.org/rtemplate) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] object literals
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: [PHP] object literals From: rob...@interjinn.com To: an_...@hotmail.com CC: f...@thefsb.org; php-general@lists.php.net 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
RE: [PHP] object literals
On Sat, 2009-05-02 at 12:39 +0200, Andrea Giammarchi wrote: 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 ... It's not what I want, I'm not the original poster. Regardless though, it comes down to the preference of the developer. It can certainly be simpler to type: $obj-blah-bleh-bluh; than to type: $obj['blah']['bleh']['blug']; Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] object literals
On Sat, 2009-05-02 at 06:45 -0400, Robert Cummings wrote: On Sat, 2009-05-02 at 12:39 +0200, Andrea Giammarchi wrote: 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 ... It's not what I want, I'm not the original poster. Regardless though, it comes down to the preference of the developer. It can certainly be simpler to type: $obj-blah-bleh-bluh; than to type: $obj['blah']['bleh']['blug']; Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP Undefined index 'blug' in $obj on line 1 ;) Ash www.ashleysheridan.co.uk
RE: [PHP] object literals
On Sat, 2009-05-02 at 12:10 +0100, Ashley Sheridan wrote: On Sat, 2009-05-02 at 06:45 -0400, Robert Cummings wrote: On Sat, 2009-05-02 at 12:39 +0200, Andrea Giammarchi wrote: 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 ... It's not what I want, I'm not the original poster. Regardless though, it comes down to the preference of the developer. It can certainly be simpler to type: $obj-blah-bleh-bluh; than to type: $obj['blah']['bleh']['blug']; Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP Undefined index 'blug' in $obj on line 1 ;) That is so weird... I got Undefined variable: obj. ;) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] object literals
On 5/2/09 6:45 AM, Robert Cummings rob...@interjinn.com wrote: It's not what I want, I'm not the original poster. as op, i think i'm going to stick with the cast. but it's been an interesting thread and i learned some useful things. Regardless though, it comes down to the preference of the developer. It can certainly be simpler to type: $obj-blah-bleh-bluh; than to type: $obj['blah']['bleh']['blug']; exactly. and i dislike typing SELECT col FROM tab WHERE a= . $a['blah'] . AND ... or even SELECT col FROM tab WHERE a={$a['blah']} AND ... when i could type SELECT col FROM tab WHERE a=$a-blah AND ... which is also easier to read. i'm really lazy about typing. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] object literals
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. -- Richard Heyes HTML5 graphing: RGraph (www.rgraph.net) PHP mail: RMail (www.phpguru.org/rmail) PHP datagrid: RGrid (www.phpguru.org/rgrid) PHP Template: RTemplate (www.phpguru.org/rtemplate) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] object literals
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 89 Developer fax: 01580 893399 Justcroft International Ltd., Staplehurst, Kent -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] object literals
Hi, You could use JSON, $foo = json_decode('{a:1,b:3}'); but I guess that's not much better than Richard's suggestion. Didn't think of that (well... it's new). That's actually much better I think, since you get the added boon of ease of portability to JS (if that's even a factor). -- Richard Heyes HTML5 graphing: RGraph (www.rgraph.net) PHP mail: RMail (www.phpguru.org/rmail) PHP datagrid: RGrid (www.phpguru.org/rgrid) PHP Template: RTemplate (www.phpguru.org/rtemplate) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] object literals
var o = { a : b, c : d }; $o = array( 'a' = b, 'c' = d ); so I guess the problem is a couple of quotes, isn't it? otherwise define object statically and externally and use json_decode ;-) Date: Thu, 30 Apr 2009 16:56:21 -0400 From: f...@thefsb.org To: php-general@lists.php.net Subject: [PHP] object literals is there a neat literal syntax for creating objects on the fly without defining a type? whenever i need to do it i do something like $x = (object) array('a'=1, 'b'=3, ...); which works but isn't very lovely. it's neater in, for example, javascript. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ Show them the way! Add maps and directions to your party invites. http://www.microsoft.com/windows/windowslive/products/events.aspx
RE: [PHP] object literals
On Fri, 2009-05-01 at 17:36 +0200, Andrea Giammarchi wrote: var o = { a : b, c : d }; $o = array( 'a' = b, 'c' = d ); so I guess the problem is a couple of quotes, isn't it? otherwise define object statically and externally and use json_decode ;-) You made an array-- not an object. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] object literals
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 From: rob...@interjinn.com To: an_...@hotmail.com CC: f...@thefsb.org; php-general@lists.php.net Date: Fri, 1 May 2009 11:40:11 -0400 Subject: RE: [PHP] object literals On Fri, 2009-05-01 at 17:36 +0200, Andrea Giammarchi wrote: var o = { a : b, c : d }; $o = array( 'a' = b, 'c' = d ); so I guess the problem is a couple of quotes, isn't it? otherwise define object statically and externally and use json_decode ;-) You made an array-- not an object. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ Show them the way! Add maps and directions to your party invites. http://www.microsoft.com/windows/windowslive/products/events.aspx
RE: [PHP] object literals
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php