Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix the PHP Strict error "Creating default object from empty value"?

Tags:

php

I have the following PHP5 code:

$request = NULL;
$request->{"header"}->{"sessionid"}        =  $_SESSION['testSession'];
$request->{"header"}->{"type"}             =  "request";

Lines 2 and 3 are producing the following error:

PHP Strict standards: Creating default object from empty value

How can I fix this error?

like image 409
Jake Avatar asked Dec 22 '09 23:12

Jake


4 Answers

Null isn't an object, so you can't assign values to it. From what you are doing it looks like you need an associative array. If you are dead set on using objects, you could use the stdClass

//using arrays
$request = array();
$request["header"]["sessionid"]        =  $_SESSION['testSession'];
$request["header"]["type"]             =  "request";

//using stdClass
$request = new stdClass();
$request->header = new stdClass();
$request->header->sessionid        =  $_SESSION['testSession'];
$request->header->type             =  "request";

I would recommend using arrays, as it is a neater syntax with (probably) the same underlying implementation.

like image 197
Yacoby Avatar answered Oct 21 '22 08:10

Yacoby


Get rid of $request = NULL and replace with:

$request = new stdClass;
$request->header = new stdClass;

You are trying to write to NULL instead of an actual object.

like image 35
Brad Avatar answered Oct 21 '22 09:10

Brad


To suppress the error:

error_reporting(0);

To fix the error:

$request = new stdClass();

hth

like image 38
Richard Quinn Avatar answered Oct 21 '22 09:10

Richard Quinn


Don't try to set attributes on a null value? Use an associative array instead.

like image 1
Amber Avatar answered Oct 21 '22 08:10

Amber