This may seem like a silly question for many of you, but it makes me wonder why PHP doesn't allow type casting to its function parameters. Many people use this method to include in their parameters:
private function dummy($id,$string){ echo (int)$id." ".(string)$string }
or
private function dummy($id,$string){ $number=(int)$id; $name=(string)$string; echo $number." ".$name; }
But, looking at many other programming languages, they accept type casting into their functional parameters. But doing this in PHP can lead to errors.
private function dummy((int)$id,(string)$string){ echo $id." ".$string; }
Parse error: syntax error, unexpected T_INT_CAST, expecting '&' or T_VARIABLE
or
private function dummy(intval($id),strval($string)){ echo $id." ".$string; }
Parse error: syntax error, unexpected '(', expecting '&' or T_VARIABLE
I just want to know why this does not work, and if there is a way. If there is no way, then the transition to the usual path is fine for me:
private function dummy($id,$string){ echo (int)$id." ".(string)$string; }
source share