How can I safely retrieve this property of this object?
If you're using PHP 7+, you can use the null coalesce operator:
'plan_id' => $subscription->items->data[0]->plan->id ?? $default,
This will evaluate the value if it's available, otherwise it will use the default, without generating any warnings or errors.
Example:
$foo = new stdClass();var_dump($foo->bar->baz->data[0]->plan->id ?? null);
Output:
NULL
You can use an isset
function over the entire selector:
isset($subscription->items->data[0]->plan->id) ? $subscription->items->data[0]->plan->id : null;
in PHP version 8
you should use Nullsafe operator as follow:
'plan_id' => $subscription?->items?->data[0]?->plan->id,