Find Available Solution Here!


Deepak Jul 22, 2024

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?


StdClass is NOT the Base class of the object. Let's see with the below example

class abc {}

$object = new abc();

if ($object instanceof stdClass) {
    echo 'Yes! It is;
} else {
     echo 'No, StdClass is NOT the Base class of the object';

//Output: 'No, StdClass is NOT the 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? The Answer is in Question too that our code is trying to access values as an 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 an 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 an array from stdclass in the following ways:

Convert stdclass to associative array using json_decode


$stdObj = new stdClass();
$stdObj->name = "Demo";
$stdObj->age = 20;

$array = json_decode(json_encode($stdObj), true);
// Json decode with second argument true will return associative arrays.

//Output Array ( [name] => Demo [age] => 20 )  


Simple objects can be converted 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 practice 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 objects. 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 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



51 Posts
  • PHP
  • JAVA
  • SEO

You May Like to Read

Scroll up