- Stdclass
- Scroll for More Useful Information and Relevant FAQs
- Is stdClass the base class of the objects?
- Why does it throw an PHP Fatal error ‘cannot use an object of type stdclass as array’ and How to fix this error?
- Ways to Convert stdclass object to an associative array in php?
- Convert stdclass to associative array using json_decode
- Typecasting
- How to fix stdclass undefined property error?
- Why stdclass undefined property issue? Let’s see with this example
- Fix stdclass undefined property issue
- Learn about Stdclass object Php foreach loop with Examples
- Was this post helpful?
- The stdClass class
- Class synopsis
- Examples
- User Contributed Notes 1 note
- What is stdClass in PHP?
- Creating stdClass Object
- Creating a stdClass Object by Type Casting
- Convert an Array into an Object
- Convert an Object into an Array
Stdclass
Stdclass is a Standard Defined Classes. Stdclass is a generic empty class created by PHP. PHP uses this class when casting.
It is just a predefined class like Object in Java. It’s not the base class of the objects.
Scroll for More Useful Information and Relevant FAQs
Is stdClass the base class of the objects?
StdClass is NOT the Base class of the object. Let’s see with below example
class abc <> $object = new abc(); if ($object instanceof stdClass) < echo 'Yes! It is'; >else < echo 'No, StdClass is NOT Base class of the object'; >//Output: 'No, StdClass is NOT Base class of the object
Why does it throw an PHP Fatal error ‘cannot use an object of type stdclass as array’ and How to fix this error?
Why did it happen? Answer is in Question too that our code is trying to access values as array even though it was object type.
Solution: Just change of accessing value from generic bracket like
$array[‘name_of_param’] To $array->name_of_param .
Let’s understand through example:
Throw Error:
// Throw Error: $array = array( 'name' => 'demo', 'age' => '21', 'address' => '123, wall street' ); $object = (object) $array; echo $object instanceof stdClass ? "Yes" : "No"; // Output : Yes //Trying to access as array echo $object['name']; // Throws : PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array .
$array = array( 'name' => 'demo', 'age' => '21', 'address' => '123, wall street' ); $object = (object) $array; echo $object instanceof stdClass ? "Yes" : "No"; // Output : Yes echo $object->name; //Output: demo
Ways to Convert stdclass object to an associative array in php?
We can convert into array from stdclass in following ways:
Convert stdclass to associative array using json_decode
name = "Demo"; $stdObj->age = 20; $array = json_decode(json_encode($stdObj), true); // Json decode with second argumen true will return associative arrays. print_r($array); //Output Array ( [name] => Demo [age] => 20 )
Typecasting
Simple objects can convert using (array) typecasting.
Example: $array = (array) $stdObj;
Complex objects or lists of objects are converted into a multidimensional array by looping through each single object with typecasting.
How to fix stdclass undefined property error?
stdclass undefined property error reported when we try to access property which does not exist/declared. It’s best practise to check whether a property is set or not. We can use the php inbuilt function isset() for such cases.
Let see with below example
Why stdclass undefined property issue? Let’s see with this example
$stdObj = new stdClass(); $stdObj->name = "Demo"; $stdObj->age = 20; echo $stdObj->notExist; // Will throw error : PHP Notice: Undefined property: stdClass::$notExist
Fix stdclass undefined property issue
$stdObj = new stdClass(); $stdObj->name = "Demo"; $stdObj->age = 20; echo isset($stdObj->notExist) ? "Property notExist is defined" : "Property notExist is NOT defined"; // Property notExist is NOT defined
Learn about Stdclass object Php foreach loop with Examples
Fetching values from stdclass object depends on object type. We don’t need looping for single dimensional object. We can fetch values like $obj->param.
We require looping through objects which is multidimensional or complex structure. Please check Stdclass object Php foreach loop for well explained about stdclass object looping with easy to understand examples.
Was this post helpful?
Feedback (optional) Please provide additional details about the selection you chose above so that we can analyze the insightful comments and ideas and take the necessary steps for this topic. Thank you
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;
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.