views:

121

answers:

8

PHP would be a lot cooler if you could write things like this:

$array = [2, 3, 5];

$object = { "name" : "Harry", "age" : 23, "cats" : ["fluffy", "mittens", "whiskers"]};

but, I just spent a lot of time looking for an extension (even an experimental alpha or anything) adding json syntax to PHP but found nothing.

Does anything like this exist?

If not, considering the existence of json_decode() and facebook's XHP, would it be difficult to write an extension to do this?

I have no experience writing PHP extensions, although I did a lot of C in college.

A: 

A mixture of associative and numerically indexed arrays usually gets you pretty close:

$object = array("name" => "Harry", "age" => 23, "cats" => array("fluffy", "mittens", "whiskers"));

In my opinion (especially because of the existence json_encode) it's not meaningfully different to write straight JSON over something like the above.

Mark E
+2  A: 

If you want to write something that isn't PHP, then use something that isn't PHP. Otherwise, use array().

$array = array(2, 3, 5);

$object = array('name' => 'Harry', 'age' => 23, 'cats' => array('fluffy', 'mittens', 'whiskers'));
Ignacio Vazquez-Abrams
Languages changes. PHP didn't have classes or closures, now it does. I find the array(=>) syntax to be needlessly verbose. I like [] and {:}
Bill
A: 

php doesn't handle json - whiwch is why it gives you the tools to encode/decode.

I you are desperate to 'write' in that manner just stick quotes around it:

$object = '{ "name" : "Harry", "age" : 23, "cats" : ["fluffy", "mittens", "whiskers"]}';

As far as php is concerned a 'json object' is nothing more than a string...

ToonMariner
+2  A: 

As people have said above, different languages have different syntax - if you don't like PHP's syntax, you might consider considering other languages.

JSON stands for "javascript object notation" - maybe what you want is server-side javascript. There's a huge array of server-side javascript options out there - jsgi/jack (narwhal), ringojs, flusspferd, node.js, v8cgi, etc. etc. http://wiki.commonjs.org/

lucideer
I don't think semantics/naming is a good reason to not have feature. Call it PHPON if you want. I'm just looking into options for improving PHP with tighter syntax.
Bill
I didn't say the name was a reason not to add it to PHP - I was just pointing out the syntax's origin in a language that you can already use on the server-side.
lucideer
@lucideer [Python](http://docs.python.org/tutorial/datastructures.html)'s list and dictionaries have similar syntax. I for one find that neater and more elegant than the `array()` construct.
quantumSoup
@Aircule, @Bill - I'm a little confused at why both of you are apparently objecting to something I haven't said at all - I haven't said anywhere that adding JSON syntax to PHP would be a bad idea, personally I do like the idea in theory. I'm merely trying to suggest helpful alternatives if the OP wants to use this syntax on the server-side today (rather than waiting for Zend to change their minds).
lucideer
@lucideer sorry, when you wrote "JSON stands for 'javascript object notation'" I thought you were being snotty and making a dumb argument. Misunderstanding.I would love to use server-side js, but I've started on a project with a large existing code base, so changing languages is not an option. Thanks for your input
Bill
A: 

yeah, if you don't want your code to be ugly, dont write it in php. Adding json will not fix that. try python or ruby

Jack
+1  A: 

Different syntax for PHP arrays has been proposed and rejected many times before.

Unfortunate, I know, because I hate the ugly syntax too.

quantumSoup
"Would take distinctness from []"? "Not searchable through search engines"? "Unreadable"? I'm speechless. I am literally *without speech*.
Ignacio Vazquez-Abrams
Pro: Readable. Con: Unreadable. AKA: "I'm not willing to admit it was a bad idea in the first place" bias. I like PHP, but there are more than a few aspects of the language that are very cumbersome.
banzaimonkey
+3  A: 

You could just wrap your datastructure in json_decode and be done with it:

$array = json_decode('[2, 3, 5]');

$object = json_decode('{
                           "name" : "Harry",
                           "age" : 23,
                           "cats" : [
                                        "fluffy", "mittens", "whiskers"
                           ]
                       }');

Yes, it doesn't do typechecking until the statement is executed, and you'll have a bit of a problem handling multiple quotes, but you could always use a HEREDOC for that.

bluesmoon
This would forgo any syntax highlighting your editor may do and make debugging that much harder. You'll also have more problems with escaping quotes. Seems like more trouble than it's worth. And for simple arrays like `array(2, 3, 5)` it's actually *more* verbose.
deceze
oh, I don't discount all the cons.
bluesmoon
A: 

Yes, PHP's array syntax is overly verbose and ugly and I too wish it would be more terse.

No, it would probably not be a good idea to attempt to change that for existing versions of PHP, since it's a feature that would need to be baked into the parser. That means your PHP applications would only run on custom compiled versions of PHP, which makes your app much less portable and thereby negate one of the good things about PHP.

You may want to attempt something like a compiler, which compiles your custom array syntax into normal syntax before running the code. If you went that far though, using an entirely different language to begin with may be the better choice.

Try to lobby for a Javascript-like syntax for PHP 6.x. Until then, just write array(). :)

deceze