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 lo开发者_高级运维t 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.
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.
Different syntax for PHP arrays has been proposed and rejected many times before.
Unfortunate, I know, because I hate the ugly syntax too.
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()
. :)
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'));
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.
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...
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/
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/)
精彩评论