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.
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.
<?php $user = array(); $user['name'] = "Rahul"; $user['email'] = "email@example.com" $user['age'] = 25; ?>
To represent same information as a properties of an object, we can use stdClass as shown below.
<?php $user = new stdClass; $user->name = "Rahul"; $user->email = "firstname.lastname@example.org"; $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.