What is stdClass in PHP?
Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.
Overview
The stdClass is a generic empty class used to cast the other type values to the object. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. The stdClass is not the base class for objects in PHP.
Example
// when we typecast one type to object we will get stdClass$obj = (object) 'Educative';var_dump($obj);// The value of the object is present in scalar propertyecho $obj->scalar;?>We type-cast the string ‘Educative’ to an object in the code above. As a result of this, we get an stdClass object. The string value will be present in the scalar property of the stdClass object.
How to convert an array to an object
$user = array('name'=>'Raju','address'=>'Hosur',);$userObj = (object) $user;var_dump($userObj);?>We create an array with the name user . Then we type-cast the array to an object. As a result of this, we get an stdClass object.
Learn in-demand tech skills in half the time
What is stdClass in PHP?
stdClass is a handy feature provided by PHP to create a regular class. It is a predefined ’empty’ class used as a utility class to cast objects of other types. It has no parents, properties, or methods. It also does not support magic methods and does not implement any interfaces.
Creating stdClass Object
In the following example, stdClass is used instead of an array to store details:
name= 'W3schools'; $obj->extension= 'In'; var_dump($object); ?>
object(stdClass)#1 (2) < ["name"]=>string(9) "W3schools" ["extension"]=> string(2) "In" >
- If an object is converted to an object using stdClass, it is not modified.
- If the given value is NULL, the new instance will also be empty.
- Arrays convert to an object with properties named by keys and associated values. It’s like the alternative to associative arrays.
- The member named scalar will contain the value for any other type of value.
Creating a stdClass Object by Type Casting
The following example shows that the value will be available in a member named Scalar when typecasting another type into an object:
object(stdClass)#1 (1) < ["scalar"]=>string(9) "W3schools" >Convert an Array into an Object
In the following example, an array is converted to an object by typecasting:
'W3schools', 'Extension'=>'In', ); $obj= (object) $obj; var_dump($obj); ?>
object(stdClass)#1 (2) < ["name"]=>string(9) "W3schools" ["Extension"]=> string(2) "In" >Convert an Object into an Array
In the following example, an object is converted to an array by typecasting:
name= 'W3schools'; $obj->extension= 'In'; $data = (array) $obj; print_r($data); ?>
Array( [name] => W3schools [extension] => In )PHP differs from other object-oriented languages because classes in PHP do not automatically derive from any class. All PHP classes are standalone unless they are explicitly extended from another class. Here you can think of defining a class that expands stdClass, but it won’t give you any benefit because stdClass does nothing.
The stdClass class
Objects of this class can be instantiated with new operator or created by typecasting to object. Several PHP functions also create instances of this class, e.g. json_decode() , mysqli_fetch_object() or PDOStatement::fetchObject() .
Despite not implementing __get()/__set() magic methods, this class allows dynamic properties and does not require the #[\AllowDynamicProperties] attribute.
This is not a base class as PHP does not have a concept of a universal base class. However, it is possible to create a custom class that extends from stdClass and as a result inherits the functionality of dynamic properties.
Class synopsis
This class has no methods or default properties.
Examples
Example #1 Created as a result of typecasting to object
The above example will output:
object(stdClass)#1 (1) < ["foo"]=>string(3) "bar" >Example #2 Created as a result of json_decode()
The above example will output:
object(stdClass)#1 (1) < ["foo"]=>string(3) "bar" >Example #3 Declaring dynamic properties
The above example will output:
object(stdClass)#1 (2) < ["foo"]=>int(42) ["1"]=> int(42) >User Contributed Notes 1 note
In PHP8 this has been changed
A number of warnings have been converted into Error exceptions:
Attempting to write to a property of a non-object. Previously this implicitly created an stdClass object for null, false and empty strings.
So if you add properties to a $var, you first need to make it a stdClass()
$var = new stdClass();
$var->propp1 = «nice»;
$var->propp2 = 1234;