Req #81231 [Opn]: improve or add short method for creating objects, it was done with arrays.

From: Date: Wed, 07 Jul 2021 20:41:50 +0000
Subject: Req #81231 [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-234890@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: Open Type: Feature/Change Request -Package: Unknown/Other Function +Package: Class/Object related Operating System: Ubuntu 20.04 PHP Version: 8.1.0alpha2 Block user comment: N Private report: N New Comment: it's an idea ... it shouldn't be a trauma. Previous Comments: ------------------------------------------------------------------------ [2021-07-07 20:27:12] icarosnet at gmail dot com I will try to explain in a better way. Currently we can declare an array and then cast it to an object in the following way: $obj1 = (object)[ 'prop1'=>'string', 'prop2'=>true, 'prop3'=>12, ]; //this option exist other way: $obj1 = new stdClass(); $obj1->prop1 = 'string'; $obj1->prop2 = true; $obj1->prop3 = 12; //this option exist I think this is done this way to save time on data development and handling; for when required ... I have implemented OOP. It seems to me that we need anonymous objects or a more obvious way to declare them, avoiding going through the new stdclass() or Array Casting; why doing object() can have the same result or if you do something like: $obj = { 'prop1'=>'string', 'prop2'=>true, 'prop3'=>12, }; //this option not exist Or $obj = object( 'prop1'=>'string', 'prop2'=>true, 'prop3'=>12, ); //this option not exist the use would be the same: $obj->prop1; the output would be: object(stdClass)#1 (3) { ["prop1"]=> string(6) "string" ["prop2"]=> bool(true) ["prop3"]=> int(12) } ------------------------------------------------------------------------ [2021-07-07 20:19:52] nospam at nospam dot com Are you familiar with object-oriented programming? I recommend reading up on it and then reading further into advanced topics of OOP and class-design. The design decisions behind using a short-syntax often lead to difficult-to-maintain or even unmaintainable code because the code typically becomes difficult to understand and fit all of it inside of one's mind. Ask yourself why you can't create a class instead, then instantiate a new object of that class? Occasionally, using a static method inside a class works wonders too. For developers, more time is spent reading and understanding code than writing code. ------------------------------------------------------------------------ [2021-07-07 20:04:57] icarosnet at gmail dot com 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? ------------------------------------------------------------------------ [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 ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=81231 -- Edit this bug report at https://bugs.php.net/bug.php?id=81231&edit=1

« previous php.bugs (#234890) next »