7

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.

Bill
  • 71
  • 1
  • 2
  • 1
    PHP uses arrays for such things. Do you find your way easier? – Hrishi Jul 17 '10 at 23:09
  • I have wanted this for a long time. Seeing that one, among many, of the inane reasons given for the decision to choose `\ ` instead of `::` for the namespace operator was the perceived difficulty of parsing `::` correctly, I doubt that this could be implemented easily. Another reason given was that `\ ` was already familiar to Windows users as a separating character, as if that were somehow important. – Jesse Dhillon Jul 17 '10 at 23:10
  • 2
    @Hrishi, even if json translated into associative arrays, that would be fine. I just strongly prefer the terse json syntax over array(=>) – Bill Jul 17 '10 at 23:22
  • Adding this to PHP isn't an option. But there might be text editors which can do more elaborate code folding (display transformation) instead of just syntax highlighting. – mario Jul 18 '10 at 03:35

8 Answers8

7

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
  • 3,918
  • 3
  • 25
  • 30
  • 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 Jul 18 '10 at 02:43
  • oh, I don't discount all the cons. – bluesmoon Jul 18 '10 at 03:28
5

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

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

Community
  • 1
  • 1
quantumSoup
  • 27,197
  • 9
  • 43
  • 57
  • 1
    "Would take distinctness from []"? "Not searchable through search engines"? "Unreadable"? I'm speechless. I am literally *without speech*. – Ignacio Vazquez-Abrams Jul 18 '10 at 02:22
  • 2
    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. – cbednarski Jul 18 '10 at 02:30
  • This is one of the reasons why PHP is loosing popularity. Syntax is really ancient – Zortext May 23 '19 at 10:13
4

Update: All the below has become somewhat moot with PHP 5.4; we now have [..] array syntax.


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
  • 510,633
  • 85
  • 743
  • 889
1

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
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    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 Jul 17 '10 at 23:20
  • Well finally PHP adopted the `[]` notation for arrays, which is syntactically way better than `array()`, so probably we will have the JSON object notation soon too. – Petruza Dec 09 '16 at 20:24
0

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 Elliot
  • 75,278
  • 22
  • 140
  • 160
0

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...

Ian Wood
  • 6,515
  • 5
  • 34
  • 73
0

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
  • 3,842
  • 25
  • 31
  • 1
    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 Jul 18 '10 at 01:38
  • 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 Jul 18 '10 at 02:09
  • [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 Jul 18 '10 at 02:23
  • @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 Jul 18 '10 at 02:50
  • 1
    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 Jul 18 '10 at 03:07
0

If php would be a lot cooler and would just let you write things like this, it would be JavaScript, I guess;) but serious:

My approach for that problem is to load json files (Get data from JSON file with PHP) and just decode them.

Maybe you could also use a build tool like Grunt/Gulp and stringyfie and inject seperate json-files in your php code. (https://florian.ec/articles/buliding-symfony2-with-gulp/)

Community
  • 1
  • 1
Phil
  • 200
  • 10