I have following PHP code:
protected $number;
public function setIntNumber( int $number ) { /* some operations */ }
public function setFloatNumber( float $number ) { /* the same operations */ }
$number
can store only int or float values. I would like to merge this two functions into one, but the main condition is that other programmer cannot enter other param than int or float. I need something like that:
public function setNumber( (int|float) $number ) { /* operations */ }
It is possible to do it EXACTLY in declaration of my function?
From the manual:
Type hints cannot be used with scalar types such as int or string. Resources and Traits are not allowed either.
Even if you could use these types, you can't use multiple types anyway. The best thing you can do is to check the values at the top of your method and throw an exception.