Built-in JSON syntax in PHP

PHP would be much 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 experimental alpha or something else) that adds json syntax for PHP, but didn't find anything.

Is there anything similar?

If not, given the existence of json_decode () and facebook XHP , would it be difficult to write an extension for this?

I have no experience writing PHP extensions, although I have done a lot in college.

+6
json syntax php
source share
8 answers

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

Unfortunately, I know, because I hate the ugly syntax .

+5
source share

You can simply wrap your data structure in json_decode and do with it:

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

Yes, it does not perform typechecking until the statement is executed, and you will have a slight problem processing several quotes, but you can always use HEREDOC to do this.

+5
source share

Update: everything below has become somewhat controversial with PHP 5.4; now we have the syntax of the array [..] .


Yes, the PHP array syntax is too verbose and ugly, and I also want it to be more concise.

No, it would be nice to try changing this for existing versions of PHP, as it is a function that needs to be baked into the parser. This means that your PHP applications will only run on custom compiled versions of PHP, which makes your application less portable and thereby negates one of the good features of PHP.

You might want to try something like a compiler that compiles your own array syntax into regular syntax before running the code. If you have come this far, though using a completely different language to start with might be the best choice.

Try lobbying for Javascript syntax for PHP 6.x. Before that just write array() . :)

+3
source share

If you want to write something that is not PHP, use something that is not PHP. Otherwise, use array() .

 $array = array(2, 3, 5); $object = array('name' => 'Harry', 'age' => 23, 'cats' => array('fluffy', 'mittens', 'whiskers')); 
+1
source share

A mixture of associative and numeric indexed arrays is usually pretty close:

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

In my opinion (especially due to the existence of json_encode ), it doesn't make sense to write direct JSON on something like the above.

0
source share

php does not handle json-whiwch, so it gives you tools for encoding / decoding.

I am desperately trying to "write" this way, just stick to the quotes around him:

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

As for php, the "json object" is nothing more than a string ...

0
source share

As mentioned above, different languages ​​have different syntax - if you do not like the PHP syntax, you can think of other languages.

JSON stands for " javascript object designation" - perhaps you need server-side javascript. There is a huge array of server side javascript parameters - jsgi / jack (narwhal), ringojs, flusspferd, node.js, v8cgi, etc. Etc. http://wiki.commonjs.org/

0
source share

If php is a lot cooler and just lets you write things like this, it will be JavaScript, I think;) but serious:

My approach to this problem is to download json files ( Get data from a JSON file using PHP ) and just decode them.

Perhaps you can also use a build tool like Grunt / Gulp and stringyfie and embed individual json files in your php code. ( https://florian.ec/articles/buliding-symfony2-with-gulp/ )

0
source share

All Articles