PHP Bugs: Property Problems

Tim Cotten
Cotten.IO
Published in
5 min readMay 6, 2021

--

PHP Notice:  Undefined property: stdClass::$TrackDetails in /app/vendor/fedex/SoapTrackClientStatus.php on line 58

PHP doesn’t mind very much if you try to access a missing property (member variable) from an object. It just throws you an E_NOTICE and returns NULL, confident that your logic will properly handle its falsey nature.

But much like it’s cousin error “Trying to get property of non-object”, it can wreck havoc when you’ve failed to prepare for them

Breach of Contract

I ran into this example while looking at an old implementation with the FedEx API (this isn’t their API code, it was a client implementation) for tracking shipments. (Names have been changed to protect the innocent.)

The nature of the error is found on lines 8, 9, and 13 where the TrackDetails object is accessed as a property of the $response object, but apparently in certain cases doesn’t exist.

What we have here is a contract failure: FedEx gave us a specification detailing what properties their responses would contain, and, for the Tracking response, we assumed it would always contain a TrackDetails object.

And you know what they say about assuming!

XKCD: “When You Assume” https://xkcd.com/1339/

Ignoring the rough and inconsistent nature of the code formatting, you can see that $return is created as an array and offsets 0 and 1 are set to $response->TrackDetails->StatusCode and $response->TrackDetails->StatusDescription respectively.

At no point, though, did we ever check to see if a TrackDetails object actually exists.

Now, we have some choices: isset() or !empty() and even property_exists().

With property_exists() you know you’re checking a class by virtue of the way the function is named, so it becomes a tempting form of self-documentation. But…

--

--

Founder of Scrypted Inc: Building interactive digital assets for the Metaverse. <tim@cotten.io> @cottenio