php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62044 Short object syntax request
Submitted: 2012-05-16 02:56 UTC Modified: 2014-12-16 23:24 UTC
Votes:8
Avg. Score:4.1 ± 1.4
Reproduced:4 of 6 (66.7%)
Same Version:4 (100.0%)
Same OS:3 (75.0%)
From: pravdin at vl dot ru Assigned: kalle (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.3 OS:
Private report: No CVE-ID: None
 [2012-05-16 02:56 UTC] pravdin at vl dot ru
Description:
------------
Thanks for short arrays syntax! :) Now it's a time to create short object syntax for simple creatation of anonymous objects with custom fields. The current shortest way is: 

$obj = (object)['field1' => 'value1', 'field2' => 'value2'];

Think it isn't beautiful :) I suggest the following syntax similar to JavaScript (which is very handly):

$obj = {{field1: 'value1', field2: 'value2'}};

or, if it possible, the same as JavaScript:

$obj = {field1: 'value1', field2: 'value2'};


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-21 17:47 UTC] duke at ejoom dot com
It will be nice to see this feature. We should have more friendly syntax.
 [2012-05-22 20:06 UTC] riptide dot tempora at opinehub dot com
I wouldn't consider this the highest priority bug/request ever, but that would definitely be great. :D
 [2012-05-28 15:12 UTC] shiranai7 at hotmail dot com
Why would you need to store plain key => value pairs as objects? If you need extra functionality for your data, make use of your own or built-in classes.

JavaScript has the object literal syntax because it distinguishses between an array - e.g. [1,2,3] - and an object literal - e.g. {name: 'Bob'}. PHP arrays can be used either way. This is why PHP has no such syntax.
 [2012-05-29 00:48 UTC] pravdin at vl dot ru
> Why would you need to store plain key => value pairs as objects?

In the same manner I can ask you why are you using arrays? Or, why are you using traits, or closures? Or, why are you using short arrays syntax? Because sometimes it is needed, and short syntax is easier and makes code more readable. In the past, we lived without closures and all program worked. And without short arrays syntax, and programs also worked :) So, this request is not a critical issue, but a step to make language more pretty and more convenient, whick makes development easier, quicker and more pleasant.
 [2014-12-16 23:24 UTC] kalle@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: kalle
 [2014-12-16 23:24 UTC] kalle@php.net
Already tried to be voted in as an RFC a while back, but declined.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 28 11:01:31 2024 UTC