php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81231 improve or add short method for creating objects, it was done with arrays.
Submitted: 2021-07-07 19:13 UTC Modified: 2021-07-07 20:52 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: icarosnet at gmail dot com Assigned:
Status: Suspended Package: Class/Object related
PHP Version: 8.1.0alpha2 OS: Ubuntu 20.04
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: icarosnet at gmail dot com
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-07 19:23 UTC] 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:29 UTC] 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 20:01 UTC] requinix@php.net
-Status: Open +Status: Suspended -Package: Unknown/Other Function +Package: Class/Object related
 [2021-07-07 20:01 UTC] 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 20:04 UTC] icarosnet at gmail dot com
-Status: Suspended +Status: Open -Package: Class/Object related +Package: Unknown/Other Function
 [2021-07-07 20:04 UTC] 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:19 UTC] 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:27 UTC] 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:41 UTC] icarosnet at gmail dot com
-Package: Unknown/Other Function +Package: Class/Object related
 [2021-07-07 20:41 UTC] icarosnet at gmail dot com
it's an idea ... it shouldn't be a trauma.
 [2021-07-07 20:52 UTC] requinix@php.net
-Status: Open +Status: Suspended
 [2021-07-07 20:52 UTC] requinix@php.net
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
    ) { }
  }
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC