Req #78494 [Sus]: Creating simple object with object()
| From: | geoffgedde at gmail dot com | Date: | Wed, 04 Sep 2019 22:00:49 +0000 |
| Subject: | Req #78494 [Sus]: Creating simple object with object() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222565@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78494&edit=1
ID: 78494
User updated by: geoffgedde at gmail dot com
Reported by: geoffgedde at gmail dot com
Summary: Creating simple object with object()
Status: Suspended
Type: Feature/Change Request
Package: *General Issues
Operating System: Any
PHP Version: 7.2.22
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2019-09-04 21:52:22] 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 21:29:43] 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 20:49:55] 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 20:31:17] 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?
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78494&edit=1