What is stdClass in PHP

Stdclass is a generic empty class which is used to type case other type value to object. we can create anonymous PHP class. Using this feature we can create a new object with its own properties.

What is stdClass in PHP

In simple words:

Stdclass is an alternative to associative array.

To use this we don’t need to write a new class template, we can simply use stdclass and create an object.

Suppose we have user information value as an associative array.

$user = array();
$user['name'] = "Rahul";
$user['email'] = "rahul@gmail.com"
$user['age'] = 25;

To represent same information as a properties of an object, we can use stdClass as shown below.

$user = new stdClass;
$user->name = "Rahul";
$user->email = "rahul@gmail.com";
$user->age = 25;


When we quickly need to create an object to hold data then stdClass is best option to use in PHP. If find this tutorial helpful, please share with your friends.

Leave a Reply

Your email address will not be published. Required fields are marked *