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

Daniel-Pablo's avatar

data_get vs elloquent

I recently see this code on one of my comrades, the code is

data_get($notification, 'fromEmployee.slack_access_token')

why is he pulling the information that way instead of doing it the way might do it... like this

$notification->fromEmployee->slack_access_token

Thanks in advance for your comments this way I can improve coding better

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

It will fallback to a default value (the third argument to data_get) more elegantly than the traditional object syntax. If fromEmployee was null, or a non-object, then the object operator syntax would not work.

Since PHP 7, we have the null coalescing operator (??), and now since PHP 8 we have null-safe object operator (?->), which means data_get is less useful. Either of the following would be acceptable depending on your PHP version:

$notification->fromEmployee->slack_access_token ?? ''
// or
$notification?->fromEmployee?->slack_access_token
1 like
Daniel-Pablo's avatar

@tykus Thank you very much for taking the time to explain this in an awesome way!! Thanks!

Please or to participate in this conversation.