Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vlchris's avatar

Lara/PHPStan - Access to undefined property (object via API)

Been scouring the interwebs but can't seem to find an answer. I am getting errors from phpstan regarding access to undefined property but it's an object created from an API call. In this instance, I'm getting an access token for an oauth call.

  187    Access to an undefined property object::$access_token.  

My question is basically how do I define this before hand? Do I need to completely build out the structure that is received first? What if it's dynamic? Just trying to figure out the right way to go about this.

0 likes
3 replies
Sinnbeck's avatar

Can you show the actual code/class? Might make it a bit easier

vlchris's avatar

So I just tried to add a ?? to it and it seemed to fix it.. I guess when you have a dynamic object like that coming from an unknown source you just need to check for the existence of it first.. either with isset, or ??

        return $tokenResponse->access_token ?? '';

Now works. Should've tried a few more things before posting but yeah, is what it is. If you get this from an object like a remote api, just check for existence first and that will make stan happy.

Sinnbeck's avatar

@vlchris you can probably also add it as a property but hard to say without seeing the code

Please or to participate in this conversation.