18

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?

2
  • 1
    Just curious, where did you see this style before? $request->{"header"}->{"sessionid"} Commented Dec 22, 2009 at 23:58
  • I saw it while framing JSON Requests. Commented Dec 23, 2009 at 0:04

4 Answers 4

42

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I couldn't get it to work with stdClass().. However, the array() style works just fine... Thank you
13

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.

Comments

4

To suppress the error:

error_reporting(0);

To fix the error:

$request = new stdClass();

hth

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.