php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53266 Shorthand object initializer
Submitted: 2010-11-08 19:28 UTC Modified: 2010-11-09 04:31 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: dukeofgaming at gmail dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: Irrelevant OS: Any (Windows 7)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dukeofgaming at gmail dot com
New email:
PHP Version: OS:

 

 [2010-11-08 19:28 UTC] dukeofgaming at gmail dot com
Description:
------------
Hi,

To often I've been in the situation where I prefer to use an object rather than 
an associative array to manage data, and in order to keep my code clean, I 
prefer to do this:

return (object)array(
  'one'=>1,
  'two'=>2,
  'three'=>3,
  'four'=>4,
);

Rather than this:

$data = new stdClass();
$data->one = 1;
$data->two = 2;
$data->three = 3;
$data->four = 4;
return $data;

AFAIK, arrays and objects are similar internally, so why not allow this:

return object(
  'one'=>1,
  'two'=>2,
  'three'=>3,
  'four'=>4,
);

Following the same rule than array to object casting:

return object(
  'one'=>1,
  'two'=>2,
  'three'=>3,
  'four'=>4,
  5
);

"5" would be ignored since its key would be numeric.

Regards,

David


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-09 04:31 UTC] aharvey@php.net
-Status: Open +Status: Wont fix -Package: SPL related +Package: Scripting Engine problem
 [2010-11-09 04:31 UTC] aharvey@php.net
Given the short syntax for arrays RFC failed, I don't see much hope of this getting up. Nevertheless, writing an RFC (http://wiki.php.net/rfc) and sending it to the internals mailing list would be the right way to propose this.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 23:01:26 2024 UTC