RE: object literals

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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: robert@xxxxxxxxxxxxx
> To: an_red@xxxxxxxxxxx
> CC: fsb@xxxxxxxxxx; php-general@xxxxxxxxxxxxx
> Date: Fri, 1 May 2009 11:40:11 -0400
> Subject: RE:  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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux