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

meredevelopment's avatar

Checking collection column isn't null before using member function

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!

0 likes
3 replies

Please or to participate in this conversation.