Maaz Zindani

Loading the index…

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

PHP warns about an undefined property on a stdClass object

ErrorWarning: Undefined property: stdClass::$email

Short answer

You read a property the object does not have. With JSON or a database row the key is simply absent in that record, or it sits one level deeper than your code assumes. Dump the object first, then read optional fields with the ?? operator or guard them with property_exists().

  • 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 object you are reading from, at the point where the warning occurs, and look at the real keys and nesting.

    <?php
    var_dump($obj);
    var_dump(array_keys(get_object_vars($obj)));
  2. 2Match what you see, then jump to its fix

    1. 1The same code works for most records and warns for a fewThe key is missing in that recordFix
    2. 2var_dump shows the key inside another object or array, such as data or userThe property is at a different nesting levelFix
    3. 3Some branches read $row['key'] and others $row->key for the same dataThe data is an array, or an object, where the code expects the otherFix

What does the error look like?

  • Warning: Undefined property: stdClass::$email

Cause 1 of 3

The key is missing in that record

1Check

JSON from an API often leaves out optional fields. Reading a property that is not there gives a warning and null; since PHP 8.0 this is a warning, not a notice.

<?php
var_dump(property_exists($obj, 'email'));
var_dump(isset($obj->email));

2Fix

Use the null coalescing operator for a default. It does not warn on a missing property. Use property_exists() when null is a valid value you need to tell apart from a missing key.

<?php
$email = $obj->email ?? 'not provided';
if (property_exists($obj, 'phone')) {
    // the key exists, even if its value is null
}

3Verify

Run the code on a record that lacks the key; no warning should appear.

php -d error_reporting=E_ALL -d display_errors=1 script.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The property is at a different nesting level

1Check

json_decode returns stdClass objects with nested stdClass objects for each JSON object. The key you want may be one level down.

<?php
$obj = json_decode($json);
var_dump($obj);

2Fix

Read the nested path as it appears in the dump. Property names that are not valid PHP identifiers need braces.

<?php
$name = $obj->user->name ?? null;
$slug = $obj->{'first-name'} ?? null;

3Verify

var_dump the value you read; it should hold the field, not null.

php script.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The data is an array, or an object, where the code expects the other

1Check

json_decode gives objects by default and arrays when its second argument is true. A cast of an array to an object makes a stdClass whose properties are the array keys.

<?php
var_dump(gettype($data), is_array($data), is_object($data));

2Fix

Pick one shape at the point of decoding and use it everywhere. Decode to arrays if you prefer array syntax.

<?php
$data = json_decode($json, true);   // arrays
$email = $data['email'] ?? null;

3Verify

Search the code for the other syntax and run the flow again.

php -l script.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Treat every field of external data as optional, and read it with ?? or a check.
  • Turn the decoded data into a typed class or array once, near where it enters your code.
  • Run development with error_reporting set to E_ALL so these warnings appear before production.
  • Use json_last_error() or JSON_THROW_ON_ERROR so a failed decode is not mistaken for missing keys.

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.