Avatar ·

What is stdClass in PHP and what is it used for?

Hello colleagues! I'm a beginner PHP developer, and when reading someone else's code, I often come across a strange construct: `$obj = new stdClass();`. Then they work with this object as a regular one, adding properties on the fly: `$obj->name = 'John';`. I understand that this is some kind of built-in class, but I can't grasp its essence.\n\nFor example, I'm used to creating objects through my own classes:\n```php\nclass User {\n public $name;\n public $email;\n}\n$user = new User();\n$user->name = 'John';\n```\nBut why use `stdClass` then? The documentation says it's an empty class, but why is it needed at all? Why can't I just use an array? After all, the array `['name' => 'John']` does the same thing.\n\nI've also seen `stdClass` appear when converting JSON to an object via `json_decode($json)`. If you don't pass the second