tags:

views:

2235

answers:

4

I'm getting an error in my PHP code Strict Standards: Creating default object from empty value. The code I'm using is:

$u = new User();
$user->id = $obj['user_id'];

The error is appearing on the second line, where I set the id property. The user class has id set as a private property with magic getters and setters defined.

Strangely, the exact same code works without the strict error elsewhere - so my main question is exactly what the error means? Hopefully then I can deduce what the problem is with the code.

+5  A: 

Shouldn't it be

$u->id = $obj['user_id'];

Your current code is creating $user as a new StdClass object.

Greg
doh ... sometimes, it's all too obvious ... :)
back2dos
Have a cup of tea
Greg
+2  A: 

Perhaps you intended

$u->id=$obj['user_id'];

You get the error because $user doesn't exist, but from the context it knows it should be an object.

Paul Dixon
+1  A: 
$u = new User();
$u->id = $obj['user_id'];

...........................

ZA
+1  A: 

Not sure if this is relevant, but in your code snippet you create a variable called $u, but access $user...

rikh