stdClass in PHP: The Magic of an Empty Object
If you've ever written code in PHP, you've probably encountered a situation: you need to quickly create an object, but you don't have a ready-made class at hand. Or you receive data from a database, and it comes as an object, but you don't understand where its properties come from. Sound familiar? Welcome to the world of stdClass — PHP's built-in class that works as an "empty container" for data.
In this article, we'll break down what stdClass is, why it's needed, and how to create and use it. You'll learn how it differs from a regular array and get practical examples you can immediately apply in your code. Let's go!
What is stdClass?
stdClass (short for "standard class") is a built-in empty class in PHP. It has no methods, properties, or predefined behavior. It exists solely so you can create an object "on the fly" without writing your own class.
Simply put, it's an anonymous object that you can fill with properties at any time. If you've ever written $obj = new stdClass();, then you've already used it.
<?php$obj = new stdClass();$obj->name = "Alexey";$obj->age = 28;echo $obj->name; // Outputs: Alexey?>In this example, we created a stdClass object and dynamically added two properties to it. No class needs to be declared in advance — PHP creates everything on its own.
Why is stdClass Needed?
At first glance, stdClass might seem like a crutch. Why use an object when you can use an array? In reality, it has several important uses:
- Working with JSON. When you decode JSON using
json_decode()without the second argument, PHP returns astdClassobject. This is the default behavior. - Temporary data structures. If you need to quickly pass a set of data as an object (for example, to a function that expects an object),
stdClassis the ideal choice. - API compatibility. Many libraries and frameworks expect objects, not arrays.
stdClassmakes it easy to adapt. - Prototyping. When you're writing a prototype and don't want to spend time creating a full-fledged class,
stdClasssaves the day.
It's important to understand: stdClass is not intended for complex logic. If you need methods, inheritance, or encapsulation — create your own class.
How to Create a stdClass Object?
You can create a stdClass object in several ways. Let's look at each.
1. Using the new operator
The most obvious way is to use new stdClass():
<?php$user = new stdClass();$user->name = "Maria";$user->email = "maria@example.com";?>2. From JSON
When decoding JSON using json_decode() (without the true flag), you get a stdClass:
<?php$json = '{"name":"Ivan","age":30}';$obj = json_decode($json);echo $obj->name; // Ivanecho get_class($obj); // stdClass?>3. Type casting
You can convert an array to an object using (object):
<?php$array = ["name" => "Olga", "city" => "Moscow"];$obj = (object) $array;echo $obj->city; // Moscow?>Note: when casting an array with numeric keys, the keys become inaccessible (PHP converts them to properties, but you'll have to access them using curly braces).
stdClass vs Array: Which to Choose?
This is an eternal question. Let's compare:
| Criterion | stdClass |
|---|