How to create an object in PHP without declaring a class?
Hi everyone! I just started learning PHP and encountered this situation. In other languages, like JavaScript, I can write something like:\n\n```javascript\nlet person = { name: "John", age: 30 };\n```\n\nThat is, create an object on the fly without predefining a class. In Python there are also dictionaries and `namedtuple`, but in PHP I see that you usually need to declare a class first:\n\n```php\nclass Person {\n public $name;\n public $age;\n}\n$person = new Person();\n$person->name = "John";\n```\n\nBut this is terribly inconvenient for simple data structures! Imagine I need to pass a configuration with 10 fields to a function — do I really have to write a class every time? I came across old forums where they recommend using `stdClass`, but I don't quite understand how it works. Is it possible to create an object without a cl
No answers yet.
Log in to leave an answer
Blogs
OAuth 2.0 and JWT Authorization in Swift: A Complete Guide for Beginners
Full URL in PHP: How to Get It Quickly and Without Errors
Traits in Rust and Typeclasses in Haskell: A Battle of Abstractions
API Gateway for Microservices in C#: Complete Guide