PHP 7 introduces return type declarations. Which means I can now indicate the return value is a certain class, interface, array, callable or one of the newly hintable scalar types, as is possible for function parameters.
function returnHello(): string {
return 'hello';
}
Often it happens that a value is not always present, and that you might return either something of some type, or null. While you can make parameters nullable by setting their default to null (DateTime $time = null
), there does not appear to be a way to do this for return types. Is that indeed the case, or am I somehow not finding how to do it? These do not work:
function returnHello(): string? {
return 'hello';
}
function returnHello(): string|null {
return 'hello';
}
function returnString(?string $stringNull) : ?string { return $stringNull;}
– StacistaciaTrowable
interface (specifically, extending theTypeError
) – Stacistacia