PHP 7.1 added nullable types to the PHP language. Here's the RFC. In short, one can specify that a return type or an argument type will accept null instead of an object (or primitive) of the specified type. This code demonstrates:
function f(int $x) : int {
return $x;
}
try {
$x = null;
$y = f($x);
} catch (\TypeError $e) {
echo $e->getMessage();
}
echo PHP_EOL . PHP_EOL;
function g(?int $x) : ?int {
return $x;
}
$x = null;
$y = g($x);
var_dump($y);
Ouptut:
Argument 1 passed to f() must be of the type integer, null given, called in [...][...] on line 8 NULL
Why do we want this? Well if we look at a statically-typed language, it makes a bit of sense:
public class NullableTest {
public static void main(String[] args){
Integer x = null;
Integer y = f(x);
System.out.println(y);
}
private static Integer f(Integer x){
return x;
}
}
So x is an Integer... it's just not been given a value yet, but it'll pass the type-check on the method signature. (This just outputs null, btw).
I think the merits of this functionality in PHP is limited when it comes to an argument type. If we had that equivalent code in PHP, it's not that x is an int that just happens to not have been given a value; it's a null. Not the same. It's a trivial but key difference I think.
Semantics aside there are occasional times where it's handy and - more importantly - appropriate. Consider the setMethods method of PHPUnit's MockBuilder class. The way this works is that one can pass one of three values to the method:
- null - no methods will be mocked-out;
- an empty array - all methods will be mocked-out;
- an array of method names - just those methods will be mocked-out.
[docs][implementation]
Here the null has meaning, and it's distinct in meaning from an empty array. Even then this only makes "sense" because for convenience the meaning of an empty array is inversed when compared to a partial array: a partial array means "only these ones", an empty array following that logic would mean "only these ones, which is none of them", but it has been given a special meaning (and IMO "special meaning" is something one ought to be hesitant about assigning to a value). Still: it's handy.
Had I been implementing PHPUnit, I probably would not have supported null there. I'd've perhaps had two methods: mockMethods(array subsetToMock) and mockAllMethods(), and if one doesn't specify either of those, then no methods are mocked. No values with special meaning, and clearly-named methods. Shrug. PHPUnit rocks, so this is not an indictment of it.
On the other hand, most of the usage I have seen of nullable arguments is this sort of thing:
function f(?int $x = 0){
if (is_null($x)){
$x = 0;
}
// rest of function, which requires $x to be an int
}
This is a misuse of the nullable functionality. This function actually requires the argument to be an integer. Null is not a meaningful value to it. So the function simply should not accept a null. It's completely legit for a function to specify what it needs, and require the calling code to actually respect that contract.
I think in the cases I have seen this being done that the dev concerned is also working on the calling code, and they "know" the calling code could quite likely have a null for the value they need to pass to this function, but instead of dealing with that in the calling code, they push the responsibility into the method. This ain't the way to do these things. A method shouldn't have to worry about the vagaries of the situation it's being used in. It should specify what it needs from the calling code to do its job, and that's it.
Another case I've seen is that the dev concerned didn't quite get the difference between "nullable" and having an argument default. They're two different things, and they're conflating the two. When an argument is nullable, one still actually needs to pass a value to it, null or otherwise:
function f(?int $x) : int {
return $x;
}
try {
$y = f();
} catch (\TypeError $e) {
echo $e->getMessage();
}
Output:
Too few arguments to function f(), 0 passed in [...][...] on line 7 and exactly 1 expected
All "nullable" means is it can be null.
I really think the occasions where a nullable argument is legit is pretty rare, so if yer writing code or reviewing someone else's code and come across one being used... stop and think about what's going on, and whether the function is taking on a responsibility the calling code oughta be looking after.
Righto.
--
Adam