Passing a null DateTime to php function -
i have function in php want accept either datetime object or null. e.g.
function foo(datetime $adate){ if ($adate == null){ echo "you passed null variable"; } else { echo "you passed date " . $adate->format('y-m-d'); } }
trouble is, because function expects datetime object flips out when pass null , gives error:
catchable fatal error: argument 1 passed foo() must instance of datetime, null given,...
how can pass null function , avoid error?
change this:
function foo(datetime $adate){
to this:
function foo($adate){
but if still want make sure object passed datetime
object can this:
function foo(datetime $adate = null){
see here more examples of type hinting.
Comments
Post a Comment