php 7.1 - PHP 7.1 Nullable Default Function Parameter -
in php 7.1 when following function called:
private function dostuff(?int $limit = 999) { }
with syntax so:
dostuff(null);
the value of $limit becomes null. guess can said value of $limit
explicitly set null
. there way overcome this? i.e. when null value (i.e. lack of value) encountered use default, whether implicit or explicit?
thanks
no php doesn't have "fallback default if null" option. should instead do:
private function dostuff(?int $limit = null) { $limit = $limit??999; // pre-int typehinting have done is_numeric($limit)?$limit:999; }
alternatively make sure either dostuff()
or dostuff(999)
when don't have sensible value doing stuff.
note: there's reflection default values of method parameters seems much.
however here's how:
$m = new reflectionfunction('dostuff'); $default = $m->getparameters()[0]->getdefaultvalue(); dostuff($default);
Comments
Post a Comment