PHP warns when you read a property from something that is not an object
Notice: Trying to get property of non-objectShort answer
The variable before the arrow holds null (or another non-object), not the object you expected. Find out why: a lookup returned nothing, or json_decode failed and returned null. Check the value first, then guard it with the nullsafe operator ?-> or an explicit check. Since PHP 8 this is a warning, not a notice.
Fix it fast
1Find out which cause you have
Dump the variable on the line named in the message. The type it prints points to the cause.
var_dump($user);2Match what you see, then jump to its fix
- 1
var_dump prints NULL for a variable that holds a database row, a model or an API result.A lookup found nothing and returned nullFix - 2
The variable came from json_decode() and var_dump shows NULL.json_decode failed and returned nullFix - 3
A second warning about an undefined variable appears on the same line.The variable was never set in this scopeFix
- 1
What does the error look like?
Notice: Trying to get property of non-objectAttempting to read a property of a non-object (a warning since PHP 8)
Cause 1 of 3
A lookup found nothing and returned null
1Check
Query helpers, repositories and array lookups commonly return null when no record matches. Reading ->name on that null triggers the message.
var_dump($repository->getUser(5));2Fix
Treat null as a normal case. The nullsafe operator (PHP 8.0 and later) returns null for the whole chain instead of reading from null; or check explicitly first.
$name = $repository?->getUser(5)?->name;
// or, explicit:
$user = $repository->getUser(5);
$name = $user !== null ? $user->name : 'guest';3Verify
Run the code path with an id that does not exist; no warning appears and you get your fallback.
php script.phpConfirmed by php.net
Cause 2 of 3
json_decode failed and returned null
1Check
The manual says json_decode returns null when the input cannot be decoded. Ask for the reason.
$data = json_decode($payload);
var_dump($data, json_last_error_msg());2Fix
Make the failure loud with JSON_THROW_ON_ERROR (PHP 7.3 and later), so a bad payload is handled at the decode, not three lines later.
try {
$data = json_decode($payload, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
// log $e->getMessage() and stop
}3Verify
Send a broken payload; the exception fires instead of a property warning.
php -r 'json_decode("{", false, 512, JSON_THROW_ON_ERROR);'Confirmed by php.net
Cause 3 of 3
The variable was never set in this scope
1Check
PHP 8 reports reading an undefined variable and reading a property of a non-object as warnings. A typo in the variable name, or a variable set only inside an if branch, leaves it undefined.
grep -n 'user' file.php2Fix
Assign the variable on every path before the arrow, or fix the name.
$user = null;
if ($id > 0) {
$user = $repository->getUser($id);
}3Verify
Run with all errors on; the undefined-variable line is gone.
php script.phpConfirmed by php.net
How do I stop it coming back?
- Give functions that can find nothing a clear return type such as ?User, so the null case is visible.
- Use ?-> for values where null is a valid answer; throw an exception where it is an error.
- Run tests and local work with error_reporting set to E_ALL.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP: Trying to access array offset on value of type null
Warning: Trying to access array offset on value of type null
You wrote $x['key'] while $x is null (or false or an int).
3 causes, each with a fixBack end
json_decode() gives NULL for JSON that looks valid
Syntax error
json_decode returns null when it cannot decode the input, and also when the JSON really is null.
4 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fix