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;}
Trowable
interface (specifically, extending the TypeError
)
PHP 7.1 Now supports nullable return types. The first RFC I linked to is the one they went for:
function nullOrString(int $foo) : ?string
{
return $foo%2 ? "odd" : null;
}
old answer:
Since my comment was actually an answer to the question:
PHP 7 won't support nullable return-types just yet, but there's an RFC out to address just that, it aims to land in PHP 7.1. If it passes, the syntax would then affect all type-hints (both return types and type-hints):
public function returnStringOrNull(?array $optionalArray) : ?string
{
if ($optionalArray) {
return implode(', ', $optionalArray);//string returned here
}
return null;
}
There's also a competing RFC to add union types, which would be able to do the same thing, but would look different:
public function returnStringOrNull(array|null $optionalArray) : string|null
{
if ($optionalArray) {
return implode(', ', $optionalArray);//string returned here
}
return null;
}
For now, though, you'll have to write:
public function returnStringOrNull( array $optionalArray = null)
{
if ($optionalArray) {
return implode(', ', $optionalArray);
}
}
Or just return an empty string to be consistent with the return type, and check falsy value:
public function returnStringOrNull( array $optionalArray = null) : string
{
if ($optionalArray) {
return implode(', ', $optionalArray);
}
return '';
}
//call
$string = $x->returnStringOrNull();
if (!$string) {
$string = $x->returnStringOrNull(range(1, 10));
}
Nullable Types are available in PHP 7.1.
This is a syntax example:
public function getName(): ?string
{
return $this->name; // name can be null
}
PHP 7.1 is now GA and you can upgrade from PHP 7.0 (there are only few backward incompatible changes that you have to check)
It works with any type. Example:
public function getOpportunity(): ?Opportunity
{
return $this->opportunity;
}
Success story sharing
PHP 7 won't support nullable return-types just yet, but there's an RFC out to address just that
- yeah, RFC, "just yet". Do not get me wrong - I'm really heavy PHP user since really crappy PHP3 times till now, no gaps, but when I saw all these RFCs they rejected for 7, my impression was just "WTF?!". Users see the mess and are willing to clean it up in the backward compatible way and they just get "no". Clean methods naming mess? Fixednull
not being too special citizen? No, not needed. Add option to make all stuff case sensitive? Nah.. And then, surprise that people switch.var
). What is a pitty is that instead, they did accept one too many nice-to-have's (spaceship operator for example)