|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2021-07-07 19:13 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 02:00:01 2025 UTC |
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.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) }Your example can be accomplished just as easily with an array, given that an anonymous/stdClass object does not support code analysis or IDE autocompletion or phpDoc tags. The only difference (besides writing [] vs. ->) is that a modification to the object will also affect the original, while with arrays any modification made would be to a copy - so actually, using an object could result in some rather nasty bugs. If you have specific named properties then you should probably be using a concrete class. Even if that class is a simple "struct" like class ThingWithProps { public string $prop1; public bool $prop2; public int $prop3; // possibly with a constructor } or, as of PHP 8.0, class ThingWithProps { public function __construct( public string $prop1, public bool $prop2, public int $prop3 ) { } }