QuizCure
Find Available Solution Here!

Stdclass

QuizCure Member Mar 19, 2023

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.

Is stdClass the base class of the objects?

No.

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 ....
   

Successfully access value using object access operator
 

$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

<?php


$stdObj = new stdClass();
$stdObj->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?

Send Feedback

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

QuizCure Member
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Other PHP Articles