W3docs

What is stdClass in PHP?

In PHP, stdClass is a predefined class, which is part of the PHP standard library

In PHP, stdClass is a predefined class, which is part of the PHP standard library. It is used as a data structure to represent an object in PHP, similar to how an object is represented in other object-oriented programming languages. An object of the stdClass class can be created using the new operator and can be accessed like any other object.

For example:

What is stdClass in PHP with an example?

<?php

$object = new stdClass;
$object->property = 'value';
echo $object->property;  // Output: value

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

stdClass objects are often used as a simple container for a set of data or as a placeholder for an object when an actual object is not required. They are also commonly used as a return value from functions or methods that return an object when the actual type of the object is not known or not relevant.