Mastering PHP stdClass: Convert Arrays, Add Dynamic Properties, and JSON Handling

This guide explains what PHP's stdClass is, how to convert arrays to objects and back, store data with dynamic properties, and use json_encode/json_decode for JSON serialization, providing clear code examples and important caveats about its inheritance behavior.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Mastering PHP stdClass: Convert Arrays, Add Dynamic Properties, and JSON Handling

Overview

stdClass

is a built‑in empty class in PHP that can be used to turn other data types into generic objects. It is similar to generic objects in Java or Python, but it is not the base class of all PHP objects.

Conversion to Object

When a non‑object value is cast to an object, PHP creates an instance of stdClass. Casting null yields an empty instance. Casting an array to object turns each key into a property name with its corresponding value. (In PHP versions prior to 7.2, numeric keys could only be accessed by iteration.)

Definition of stdClass

stdClass is a base class that almost every class can inherit from; it can be instantiated at any time to turn a variable into an object.

Variables created with new stdClass cannot call methods, so expressions like $a->text() are impossible.

stdClass became popular starting with PHP 5; it should be avoided in versions earlier than PHP 5.

Typical Uses of stdClass

Directly accessing members by creating dynamic objects.

Useful for building objects on the fly.

Setting dynamic properties without pre‑defining a class.

Practical Usage of stdClass

1. Storing Data

(1) Array storage
$personal_array = [
    "name" => "Tinywan",
    "home" => "www.tinywan.com",
    "address" => "ZheJiang HangZhou"
];
print_r($personal_array);

Output shows a regular PHP array.

(2) stdClass storage (dynamic properties)
$personal_object = new stdClass();
$personal_object->name = "Tinywan";
$personal_object->home = "www.tinywan.com";
$personal_object->address = "ZheJiang HangZhou";
print_r($personal_object);

The result is a stdClass object with the same fields.

(3) Converting an array to an object
$personal_array = [
    "name" => "Tinywan",
    "home" => "www.tinywan.com",
    "address" => "ZheJiang HangZhou"
];
$personal_object = (object) $personal_array;
print_r($personal_object);
(4) Converting an object back to an array
$personal_object = new stdClass();
$personal_object->name = "Tinywan";
$personal_object->home = "www.tinywan.com";
$personal_object->address = "ZheJiang HangZhou";
$personal_array = (array) $personal_object;
print_r($personal_array);

2. Dynamically Adding Properties

$personal_object = new stdClass();
$personal_object->name = "Tinywan";
$personal_object->home = "www.tinywan.com";
$personal_object->address = "ZheJiang HangZhou";
print_r($personal_object);

// Dynamically add properties
$personal_object->age = 24;
$personal_object->schoole = "GanZhengFa";
print_r($personal_object);

The second printout shows the newly added age and schoole properties.

stdClass is not the base class of all PHP objects

Even though stdClass is a generic class, it is not the root of the PHP object hierarchy. This can be demonstrated with the instanceof operator:

class Tinywan {}

$objClass = new Tinywan();
if ($objClass instanceof \stdClass) {
    echo 'Yes';
} else {
    echo 'No';
}

The output is No, confirming that stdClass is not the universal base class.

3. json_encode() and json_decode()

The functions json_encode() and json_decode() are used to serialize arrays or objects to JSON strings and back. By encoding an array and then decoding it, PHP creates a stdClass object representation of the original data.

$empInfo = [
    'name' => 'John',
    'address' => 'Houston',
    'employment' => [
        'id' => '1',
        'address' => 'Los Angeles'
    ]
];
print_r(json_decode(json_encode($empInfo)));

The resulting structure is a nested stdClass object where the inner employment array is also converted to a stdClass instance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JSONPHPobjectdynamic propertiesarray conversionstdClass
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.