rfc:objectarrayliterals

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:objectarrayliterals [2011/06/06 16:52] – add header seanrfc:objectarrayliterals [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ===== RFC: First-Class Object and Array Literals ===== ===== RFC: First-Class Object and Array Literals =====
  
-  * Version: 1.0 +  * Version: 1.0.3 
-  * Date: 2011-06-04+  * Date: 2011-06-04 (Updated: 2011-06-06)
   * Author: Sean Coates <sean@php.net>   * Author: Sean Coates <sean@php.net>
-  * Status: Under Discussion+  * Status: Inactive
  
 ==== Introduction ==== ==== Introduction ====
Line 23: Line 23:
 This proposal calls for first-class JSON-like object and array literals This proposal calls for first-class JSON-like object and array literals
 (a.k.a. primitives, constructs) in PHP. (a.k.a. primitives, constructs) in PHP.
 +
 +This RFC supersedes the existing [[rfc:shortsyntaxforarrays|Short Syntax for Arrays RFC]].
  
  
Line 41: Line 43:
 Furthermore, JSON-proper is a serialization format, but this proposal Furthermore, JSON-proper is a serialization format, but this proposal
 concerns a declaratative, inline, interpreted object literal format. concerns a declaratative, inline, interpreted object literal format.
 +
 +In JSON-proper, keys must be quoted with ''"'' and not ''%%'%%''. This same behaviour should not be expected in PHP. Similarly, PHP arrays allow a trailing comma after the last element, but JSON does not allow this. The existing PHP behaviour should be allowed in these respects.
  
 Point of discussion (see below): the key-value separator will be both PHP's Point of discussion (see below): the key-value separator will be both PHP's
Line 100: Line 104:
 ?> ?>
 </code> </code>
 +
 +=== Interaction with third-party services that speak JS-literals ===
 +<code php>
 +<?php
 +// Here's how an ElasticSearch query currently looks in PHP:
 +
 +$esQuery = new \StdClass;
 +$esQuery->query = new \StdClass;
 +$esQuery->query->term = new \StdClass;
 +$esQuery->query->term->name = 'beer';
 +$esQuery->size = 1;
 +
 +// OR
 +
 +$esQuery = (object)array(
 +   "query" => (object)array(
 +       "term" => (object)array(
 +           "name" => "beer"
 +       )
 +   ),
 +   "size" => 1
 +);
 +
 +// …and here's how it could look with the proposed syntax:
 +
 +$esQuery = {
 +   "query" : {
 +       "term" : {
 +           "name": "beer"
 +       }
 +   },
 +   "size" : 1
 +};
 +
 +/*
 +…and here's how I'd use curl to ensure that the query I'm issuing does in fact work with ElasticSearch:
 +
 +$ curl http://localhost:9200/gimmebar/assets/_search?pretty=1 -d'{
 +   "query" : {
 +       "term" : {
 +           "name": "beer"
 +       }
 +   },
 +   "size" : 1
 +}'
 +*/
 +</code>
 +
 +Even considering the ''(object)array('' syntax, it's much easier to work with an external query (as shown with curl), if we have a (nearly) JSON-compatible syntax in PHP.
 +
 +Note that this could have been written with the PHP definition of $esQuery in the proposed yet non-JSON compatible syntax (single quotes, for example), but it was written with double quotes because it is easier to pass off to curl.
 +
 +Realistically, "beer" would be in a variable (maybe ''{"term": {"name": $term}}''), but replacing just the variables is certainly much easier than translating the ''new \StdClass'' syntax.
 +
 +The argument for right-hand-side assignments being allowed in the proposed syntax (such as in ''{'time': time()}'') is still valid because it is expected that this syntax will be used both for interacting with third party services (as with ElasticSearch above), but also generally for object and array creation without a care about third parties.
 +
  
 ==== Benefits ==== ==== Benefits ====
Line 148: Line 208:
   * Performance concerns: encoding/re-encoding, memory usage.   * Performance concerns: encoding/re-encoding, memory usage.
  
 +==== Patch ====
 +  * A partial (arrays only, colons only) [[http://www.opendogs.org/pub/php-5.3dev-080109-sbar.patch|patch]] is available.
  
 ==== Discussions ==== ==== Discussions ====
rfc/objectarrayliterals.1307379153.txt.gz · Last modified: 2017/09/22 13:28 (external edit)