DEV Community

Cover image for New in PHP 8! ๐’๐ข๐ฆ๐ฉ๐ฅ๐ข๐Ÿ๐ฒ ๐˜๐จ๐ฎ๐ซ ๐‚๐จ๐๐ž ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ
Irpan Abdul Rahman
Irpan Abdul Rahman

Posted on

1

New in PHP 8! ๐’๐ข๐ฆ๐ฉ๐ฅ๐ข๐Ÿ๐ฒ ๐˜๐จ๐ฎ๐ซ ๐‚๐จ๐๐ž ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ

The Nullsafe operator, introduced in PHP 8.0, is a game-changer for handling nullable properties and method calls more gracefully. It allows you to avoid verbose null checks, making your code cleaner and more readable.

Example Tradition Null Check

$userCountry = null;
if ($user !== null) {
   if ($user->getAddress() !== null) {
      $userCountry = $user->getAddress()->getCountry();  
   }
}
Enter fullscreen mode Exit fullscreen mode

๐–๐ก๐ฒ ๐”๐ฌ๐ž ๐ญ๐ก๐ž ๐๐ฎ๐ฅ๐ฅ๐ฌ๐š๐Ÿ๐ž ๐Ž๐ฉ๐ž๐ซ๐š๐ญ๐จ๐ซ?

โœ… ๐‚๐จ๐ง๐œ๐ข๐ฌ๐ž๐ง๐ž๐ฌ๐ฌ: Reduces the amount of boilerplate code required for null checks.
โœ… ๐‘๐ž๐š๐๐š๐›๐ข๐ฅ๐ข๐ญ๐ฒ: Makes your code more readable and expressive, clearly showing the intent of handling nullable values.
โœ… ๐’๐š๐Ÿ๐ž๐ญ๐ฒ: Helps avoid null dereference errors in a more elegant way, ensuring your code handles potential null values seamlessly.

Nullsafe implementation

$userCountry = $user?->getAddress()?->getCountry();
Enter fullscreen mode Exit fullscreen mode

Have you started using the Nullsafe operator in your PHP 8 projects? Share your thoughts and experiences!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (2)

Collapse
 
xwero profile image
david duymelinck โ€ข

Although this is a great feature, using null is considered as a code smell these days.
The main reason that it became a code smell is that php now has return type hinting. And it is better to return a single type instead of multiple.

It is also better to do an early return. so instead of a waterfall of checks just do one check

$userCountry = $user !== null && $user->getAddress() !== null ? $user->getCountry() : '';
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bentesolution profile image
Irpan Abdul Rahman โ€ข

Thank you @xwero You raise valid points regarding the use of null and the importance of return type hinting in PHP. I agree that returning a single type and utilizing early returns can lead to cleaner and more maintainable code

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

๐Ÿ‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Communityโ€”every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple โ€œthank youโ€ goes a long wayโ€”express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay