Is it possible to define the return type like this?
public static function bool Test($value)
{
return $value; //this value will be bool
}
Since this question still comes up in search engine results, here is an up-to-date answer:
PHP 7 actually introduced proper return types for functions / methods as per this RFC.
Here is the example from the manual linked above:
function sum($a, $b): float {
return $a + $b;
}
Or, in a more general notation:
function function_name(): return_type {
// some code
return $var // Has to be of type `return_type`
}
If the returned variable or value does not match the return type, PHP will implicitly convert it to that type. Alternatively, you can enable strict typing for the file via declare(strict_types=1);
, in which case a type mismatch will lead to a TypeError Exception.
Easy as that. However, remember that you need to make sure that PHP 7 is available on both, your development and production server.