PHP warns about an undefined property on a stdClass object
Warning: Undefined property: stdClass::$emailShort 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().
Fix it fast
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)));2Match what you see, then jump to its fix
- 1
The same code works for most records and warns for a fewThe key is missing in that recordFix - 2
var_dump shows the key inside another object or array, such as data or userThe property is at a different nesting levelFix - 3
Some 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
- 1
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.phpConfirmed by php.net
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.phpConfirmed by php.net
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.phpConfirmed by php.net
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
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 fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix