:::: MENU ::::
Posts tagged with: unknown

Manual Object in PHP5 (stdClass and OOP)

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;

Enjoy