stdClass in PHP: what it is and how to use it in practice

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is stdClass in PHP? Understanding the base object

\n\n

If you've ever worked with PHP and encountered an unexpected object where you expected an array, or saw the mysterious stdClass in the debugger — you're not alone. This small but important class raises many questions among developers. In this article, we'll break down what stdClass in PHP is, how it works, and most importantly, why it's needed in real projects.

\n\n

Let's set the record straight right away: stdClass in PHP is not some magical or complex tool. It's a simple built-in class that serves as an empty container for creating objects "on the fly." It can be compared to a blank sheet of paper: you decide what to write on it.

\n\n

How did stdClass come about and why is it needed?

\n\n

PHP is a dynamically typed language, and data often comes in the form of arrays. But what if you need an object instead of an array? For example, when working with JSON, databases, or external APIs. This is where stdClass PHP comes to the rescue — a "dummy" that allows you to quickly create an object without declaring your own class.

\n\n

Imagine a scenario: you receive data from an external service in JSON format. PHP decodes it into a stdClass object if you don't specify the second parameter true. This is convenient because objects in PHP behave more predictably than arrays in some scenarios.

\n\n

Step-by-step guide: how to create and use stdClass

\n\n

Step 1: Creating a stdClass object

\n\n

The simplest way is to use the new keyword:

\n\n
<?php\n// Create an empty stdClass object\n$obj = new stdClass();\n\n// Add properties on the fly\n$obj->name = 'Anna';\n$obj->age = 28;\n$obj->is_active = true;\n\nvar_dump($obj);\n?>
\n\n

Result: you get a full-fledged object with three properties. No need to declare any classes — stdClass in PHP allows you to do this instantly.

\n\n

Step 2: Casting an array to an object

\n\n

One of the most useful features is converting an array to stdClass. This is often encountered when working with data:

\n\n
<?php\n// Original array\n$user = [\n    'login' => 'ivan123',\n    'email' => 'ivan@example.com',\n    'role' => 'admin'\n];\n\n// Cast to object\n$userObj = (object) $user;\n\necho $userObj->login; // 'ivan123'\necho $userObj->email; // 'ivan@example.com'\n?>
\n\n

Note: array keys become object properties. If a key contains invalid characters (e.g., a hyphen), PHP will create the property, but you won't be able to access it directly — you'll need to use curly braces.

\n\n

Step 3: Working with JSON

\n\n

The most common use case for stdClass PHP is decoding JSON. By default, the json_decode() function returns a stdClass object:

\n\n
<?php\n$json = '{"title":"What is stdClass","views":15400,"published":true}';\n$data = json_decode($json);\n\n// $data is a stdClass object\necho $data->title;   // 'What is stdClass'\necho $data->views;   // 15400\n\n// If you need an array, pass true as the second parameter\n$array = json_decode($json, true);\necho $array['title']; // 'What is stdClass'\n?>
\n\n

As you can see, stdClass in PHP allows you to work with JSON data as objects, which is often more convenient than arrays — especially in method chains or when passing data to functions that expect objects.

\n\n

Advanced techniques for working with stdClass

\n\n

When is stdClass better than a custom class?

\n\n

Many developers debate: should you use stdClass or always declare your own class? Here are a few situations where stdClass in PHP is truly appropriate:

\n\n
    \n
  • Rapid prototyping — when you need to create an object "here and now" without extra

Blogs

Book Recommendations