Avatar ·

What is stdClass in PHP and why is it needed?

Hello everyone! I started learning PHP and came across a strange thing — stdClass. I see it in different places but can't understand what it is. For example, I used json_decode() and got some object. When I tried to check its type, I was told it's stdClass. Here's an example:\n\n```php\n$json = '{"name":"John","age":30}';\n$obj = json_decode($json);\nvar_dump($obj); // object(stdClass)#1 (2) { ["name"]=> string(4) "John" ["age"]=> int(30) }\n```\n\nI tried to create such an object myself but didn't understand how. I wrote `$obj = new stdClass;`, and it seems to work. But why? After all, there are regular arrays. How is stdClass different from a regular array? And why does PHP use this thing at all instead of just arrays? Tell me what kind of beast this is and where to use it correctly. Otherwise, I'm afraid I'm writing something wrong.