Why typecasting is not an option in PHP function parameters

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; } 
+6
source share
3 answers

PHP has a rudimentary hint-type ability for arrays and objects, but it does not work on scalar types.

PHP 5 introduces type hints. Functions can now force objects to be objects (by specifying the class name in the function prototype), interfaces, arrays (starting with PHP 5.1) or called (starting with PHP 5.4). However, if NULL is used as the default parameter value, it will be resolved as an argument for any subsequent call.

If a class or interface is specified as a type hint, then all its children or implementations also allow> ed.

Tooltip types cannot be used with scalar types such as int or string. Traits are also not allowed.

Array tooltip example:

 public function needs_array(array $arr) { var_dump($arr); } 

Object tooltip example

 public function needs_myClass(myClass $obj) { var_dump($obj); } 

If you need to enforce a scalar type, you will need to do this by specifying or checking the type of the function and performing its action or acting accordingly if it gets the wrong type.

Throw exceptions if you're wrong

 public function needs_int_and_string($int, $str) { if (!ctype_digit(strval($int)) { throw new Exception('$int must be an int'); } if (strval($str) !== $str) { throw new Exception('$str must be a string'); } } 

Just silently enter the parameters

 public function needs_int_and_string($int, $str) { $int = intval($int); $str = strval($str); } 
+8
source

PHP only allows this for object types, and I suspect it because the language is so poorly typed otherwise. Consider this:

 <?php class Foo { public function dummy(int $id, string $string){ echo $id." ".$string; } } $foo = new Foo(); $foo->dummy(1, "me"); 

This code is syntactically correct (i.e. compiles), however there is a runtime error:

Fatal error allowed: argument 1 passed to Foo :: dummy () must be an int, integer given instance, called in /Users/christrahey/tes.php on line 11 and defined in / Users / christrahey / tes. php on line 4

Note that it is looking for an instance of a class named int .

+3
source

For all the people coming here from Google, you probably know that now with php 7.x you can declare all types of parameters, including scalars:

 <?php declare(strict_types=1); function foo(int $i){ echo $i; } foo(20); //foo('abvc'); //Fatal error: Uncaught TypeError: Argument 1 passed to foo() must be of the type integer, string given foo('12');//this will be OK, *unless* we enable strict types at the first line 

More details:

http://php.net/manual/en/migration70.new-features.php

0
source

Source: https://habr.com/ru/post/920466/


All Articles