php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78494 Creating simple object with object()
Submitted: 2019-09-04 20:31 UTC Modified: 2019-09-04 22:00 UTC
From: geoffgedde at gmail dot com Assigned:
Status: Suspended Package: *General Issues
PHP Version: 7.2.22 OS: Any
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: geoffgedde at gmail dot com
New email:
PHP Version: OS:

 

 [2019-09-04 20:31 UTC] geoffgedde at gmail dot com
Description:
------------
Many times I prefer the syntax and usage of Objects over Arrays, but creating objects quickly is awkward and not very readable to the entire Team. However, it would be simplified if we could use a function called object() like we do with array().  Sure we could typecast array with (object) but that could be confusing to other devs if they miss-read or typecast.

Instead it would be better to do:
$obj = object(
   'name' => 'Some Name',
   'id' => 123
);

echo $obj->name;

object() is an unused function so why can't it be added in?


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-04 20:49 UTC] requinix@php.net
-Status: Open +Status: Suspended
 [2019-09-04 20:49 UTC] requinix@php.net
This would have to be a new *syntax*, not function. As such this is a subject that needs to go through the RFC process, which you can learn about at https://wiki.php.net/rfc/howto.


But honestly, I don't see the difference between

  object(
    'name' => 'Some Name',
    'id' => 123
  )

and

  (object)[
    'name' => 'Some Name',
    'id' => 123
  ]

Or if you really wanted a function for the sake of... appearance? then,

  function object(array $properties) { return (object)$properties; }

  object([
    'name' => 'Some Name',
    'id' => 123
  ])

Which is to say nothing about whether your use case would be best addressed by a real class instead of stdClass.
 [2019-09-04 21:29 UTC] geoffgedde at gmail dot com
Thanks requinix,
Yeah I do use the method:
object([
    'name' => 'Some Name',
    'id' => 123
  ])

This is more of a "If object() is available why not use it?" question.
 [2019-09-04 21:52 UTC] requinix@php.net
"Why not" is a good reason for trying a new food or asking someone out on a date. It is not a good reason for making syntax changes to a language. There is more to consider here than you may think.
 [2019-09-04 22:00 UTC] geoffgedde at gmail dot com
requinix, agreed.
I am merely starting a conversation. For me it would be nice to have. Less syntax and more consistency. I also think more readable, but those are just my opinions.

Also, I remember trying to use it a long time ago and getting a function not found error and thinking that something was broken as I assumed that it should have been available.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Nov 26 16:01:23 2020 UTC