Level 5
PHP 8 allows you to write this:
if ($item->item_expiry_date?->isPast()) {
// do abort thing.
}
More Information on: PHP 8: the null safe operator - stitcher.io
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I want to do this, which checks If column item_expiry_date (which is cast to date) is in the past:
if ($item->item_expiry_date->isPast()) {
// do abort thing.
}
...but sometimes item_expiry_date is null, which means I get 'Call to a member function isPast() on null".
So at the moment I'm doing something like this:
$itemexpdate = $item->item_expiry_date;
if ($itemexpdate !== null) {
if ($itemexpdate->isPast()) {
// do abort thing.
}
}
Which works ok. But is there a neater / shorter / better way? I hoped I could inline the 'if not null' type check.
Cheers!
Please or to participate in this conversation.