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

criber's avatar

Undefined property: stdClass in JsonResource

Hey guys, I am hassling with some JsonResource and did not find any relevant information on that topic.

First some information about the planned implementation:

The resource looks like this:

class User extends Resource
{
    public function toArray($request)
    {
        return [
            'id' => $this->_id,
            'email' => $this->email,
            'firstName' => $this->first_name,
            'lastName' => $this->last_name,
        ];
    }
}

When I fetch all users of a specific workspace from taskworld, I get the following error:

ErrorException
Undefined property: stdClass::$first_name

According to the api they should define the 4 fields (_id, email, first_name, last_name), but my tests show one system-user without first_name and last_name. That will be the reason, why the exception is triggered.

I already tried to check for existence of a property and to alter the Method 'toArray' like this:

class User extends Resource
{
    public function toArray($request)
    {
        if(!defined($this->first_name) && !defined($this->last_name)){
            return [
                'id' => $this->_id,
                'email' => $this->email,
                'first_name' => null,
                'last_name' => null,
            ];
        }else{
            return [
                'id' => $this->_id,
                'email' => $this->email,
                'first_name' => $this->first_name,
                'last_name' => $this->last_name,
            ];
        }
    }
}

That does also trigger the exception.

How can I prevent this behaviour?

Thanks in advance :-)

Kind regards, Cris

0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Give this a try

class User extends Resource
{
    public function toArray($request)
    {
        return [
            'id' => $this->_id,
            'email' => $this->email,
            'firstName' => $this->first_name ?? null,
            'lastName' => $this->last_name ?? null,
        ];
    }
}
1 like
criber's avatar

Thanks for the fast response. I checked DelegatesToResource and found __isset(). That worked for me :-)

Here my solution for other interested parties:

class User extends Resource
{
    public function toArray($request)
    {
        $firstName = $this->__isset('first_name') ? $this->first_name : null;
        $lastName = $this->__isset('last_name') ? $this->last_name : null;

        return [
            'id' => $this->_id,
            'email' => $this->email,
            'first_name' => $firstName,
            'last_name' => $lastName,
        ];
    }
}
criber's avatar

@sinnbeck I was curious if your solution works and successfully tested it.

It's a lot cleaner :-) Thanks again for your effort.

Sinnbeck's avatar

Happy to help. ?? is shorthand for isset in php7.2 and up

criber's avatar

@sinnbeck Working with the non-resolved object, accessing the missing elements threw the exception again.

My solution for this case was some work around to prevent the error, if the property is missing:

class User extends Resource
{
    public function toArray($request)
    {
        return [
            'id' => $this->_id,
            'email' => $this->email,
            'first_name' => $this->first_name ?? null,
            'last_name' => $this->last_name ?? null,
        ];
    }

    public function __get($key)
    {
        if (($key == 'first_name' || $key == 'last_name') && !$this->__isset($key)) {
            return null;
        } else {
            return parent::__get($key);
        }
    }
}

This code prevents any possible error for the two known 'optional' fields :-)

Thanks again!

Please or to participate in this conversation.