Req #81231 [Sus->Opn]: improve or add short method for creating objects, it was done with arrays.
| From: | icarosnet at gmail dot com | Date: | Wed, 07 Jul 2021 20:04:57 +0000 |
| Subject: | Req #81231 [Sus->Opn]: improve or add short method for creating objects, it was done with arrays. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-234887@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81231&edit=1
ID: 81231
User updated by: icarosnet at gmail dot com
Reported by: icarosnet at gmail dot com
Summary: improve or add short method for creating objects, it
was done with arrays.
-Status: Suspended
+Status: Open
Type: Feature/Change Request
-Package: Class/Object related
+Package: Unknown/Other Function
Operating System: Ubuntu 20.04
PHP Version: 8.1.0alpha2
Block user comment: N
Private report: N
New Comment:
why an object must be an instance of a class;
and cannot be a native type; being able to create an object as an array is created can help just as
the method of creating short arrays has helped.
the question should not be why; the question should be why not implement something like that?
Previous Comments:
------------------------------------------------------------------------
[2021-07-07 20:01:00] requinix@php.net
Proposed and discussed a few times. But reconsider whether you really need so many stdClass objects
or whether some concrete types would be better.
Note that [] isn't a really shorthand: both array syntaxes look and work the same way, except
one starts with "[" and ends with "]" while the other starts with
"array(" and ends with ")". Meanwhile objects have no corresponding syntax at
all - long or short.
Perhaps that's because stdClass is a second-class citizen (pun intended) after arrays and
"real" classes. And while they're commonly used for comparisons, objects in
Javascript are more closely related to PHP's associative arrays; PHP's objects are closer
to traditional OOP classes, plus some added dynamic features.
------------------------------------------------------------------------
[2021-07-07 19:29:40] nospam at nospam dot com
But...why?
An object is an instance of a class. There are anonymous functions for closures. A request like this
should be discussed on internals, and has been discussed on internals in the past:
https://externals.io/message/106947
------------------------------------------------------------------------
[2021-07-07 19:23:34] fffr dot fffg at hf dot dd
what problem do you want to solve?
you don't need the () at all unless you want to provide parameters for a constructor - so you
save exactly one char for what benefit?
------------------------------------------------------------------------
[2021-07-07 19:13:29] icarosnet at gmail dot com
Description:
------------
improve or add short method for creating objects, it was done with arrays.
Test script:
---------------
The only two ways to create an object in PHP are:
$obj = new stdclass ();
$obj = (object) Array(); // the same as $ obj = (object)[];
or am I wrong?
why objects don't have a short initialization method:
$obj = Object(); // avoiding the use of new stdclass ();
$obj = {};
similar to how an array is created
$arr = Array();
$arr = [];
Actual result:
--------------
Error
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81231&edit=1