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
 [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

Pull Requests

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: Sat Dec 28 11:01:31 2024 UTC