Yes, now itβs possible. After a long discussion, the proposal to introduce a type hint for scalar function parameters and return values ββwas only approved with the highest vote count, check in more detail:
A Scalar type hint consists of declaring function parameter types and return values, which can be of types int, float, string and bool. This allows the PHP runtime engine to check whether value types have been passed to parameter functions and returned values ββhave the expected types to detect possible programming errors. The hint type for objects, arrays, and callables is already allowed in previous versions of PHP. The current implementation introduces five new reserved words: int, float, bool, string and numeric. They were not previously reserved since casting is a special case in a lexer.
Example : function test(float $a) { var_dump($a); } test(1); // float(1) test("1"); // float(1) test(1.0); // float(1) test("1a"); // E_RECOVERABLE_ERROR test("a"); // E_RECOVERABLE_ERROR test(""); // E_RECOVERABLE_ERROR test(1.5); // float(1.5) test(array()); // E_RECOVERABLE_ERROR test(new StdClass); // E_RECOVERABLE_ERROR
You also have the option to declare to the source file, where you can enable the use of the Scaler type. It must be the 1st line of your script configuration and cannot be declared anywhere else in the same file.
Like : declare(strict_types=1);
At run time, when the PHP engine tries to return a value, it will check if it does not match it, as it is declared that it will generate a fatal error, for example, Fatal error: argument 1 passed to increment () must be of type integer, string given
Thanks to these new declaration functions, you can write more robust applications by detecting early programming errors caused by passing values ββof the wrong types to functions.
Automatic type changes are also possible. For example, int types can automatically change in parameters of type float,
function test(float $x){ var_dump($x); } test(10); // works fine
Return Type Declaration
We can declare return types by adding a colon followed by the expected type between the last bracket and the first bracket in the function declaration.
For functions that do not return any value, nothing should be added to the declaration section of the return type.
function mustReturnInt(): int { ... } function mustReturnString(): string { ... } function mustReturnBool(): bool { ... } function mustReturnFloat(): float { ... } function doesNotReturnAnything() { ... }
A bit more complicated example
declare(strict_types=1); class StrictTypesTestingClass { public function returnSameInt(int $value): int { return $value; } public function returnSameFloat(float $value): float { return $value; } public function returnSameString(string $value): string { return $value; } public function returnSameBool(bool $value): bool { return $value; } } $check = new StrictTypesTestingClass(); // calls that work print $check->returnSameInt(10); print $check->returnSameFloat(10.0); print $check->returnSameString("test"); print $check->returnSameBool(true) ? 'true' : 'false'; // calls that throw exceptions print $check->returnSameInt("10"); print $check->returnSameFloat("10.0"); print $check->returnSameString(10); print $check->returnSameBool("true");
Weak type checking and type conversion behavior: weak type checking mode can be used with declaration statement (strict_types = 0); or lack of declaration of strict types. There are a few things to consider: Weak types of checked calls to an extension or built-in PHP function have the same behavior as in previous versions of PHP. Weak type checking rules for new scalar type declarations are basically the same as for extension functions or PHP built-in functions. NULL is a special case to match the current type declarations for classes, called and arrays. NULL is not accepted by default, unless it is a parameter and is explicitly set to NULL by default, for example: function sample (int $ a = NULL);
There are many advantages to this approach. You get type safety. This means that you can finally statically analyze the code! You may find errors when you accidentally take a string from one function and pass it as an integer to another. For me, a developer who uses PHP daily and sees Java as a reference for OOP languages, this is a big advance for PHP.