I have a function in php that I want to accept either a DateTime object or null. E.g.
function foo(DateTime $aDate){
if ($aDate == null){
echo "You passed a null variable";
} else {
echo "You passed the date " . $aDate->format('Y-m-d');
}
}
Trouble is, because the function expects a DateTime object it flips out when I pass null to it and gives an error:
Catchable fatal error: Argument 1 passed to foo() must be an instance of DateTime, null given,...
How can I pass null to the function and avoid this error?