11 December 2011

Background: NULL Object

When you have a NULL object and try to manually assign a variable to it, such as, $object->user->name, but $object->user is not a class object, you will receive the following error:

[Notice]: Trying to get property of non-object in [file]. To fix this, follow the few simple steps below:

The Code: Creating an Object

<?php 
$object = new stdClass;
$object->data = (object)array(); 
$object->data->item = 'item'; 
echo $object->data->item;

Meta