Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP warns when you read a property from something that is not an object

ErrorNotice: Trying to get property of non-object

Short 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1var_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. 2The variable came from json_decode() and var_dump shows NULL.json_decode failed and returned nullFix
    3. 3A second warning about an undefined variable appears on the same line.The variable was never set in this scopeFix

What does the error look like?

  • Notice: Trying to get property of non-object
  • Attempting 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.php

Confirmed by php.net

Did this fix it?

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

Did this fix it?

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.php

2Fix

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.php

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.