Why typecasting is not an option in PHP function parameters

Viewed 8217

This might sound like a silly question to many of you, but it keeps me wondering why PHP does not allow typecasting in its function parameters. Many people use this method to cast into 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 typecasting into their function parameters. But doing this in PHP may 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

Just want to know why this does not work and if there is a way. If there is no way, then going to the regular way is alright for me:

private function dummy($id,$string){
    echo (int)$id." ".(string)$string;
}
3 Answers
Related