Nullsafe Operator doesn't work with uninitialized properties
Description
The following code:
<?php
class SomeClass {
public DateTime $date_val;
}
$some = new SomeClass;
// Test 1
echo $some->date_val?->format("Y-m-d") ?? "default";
// Test 2
echo $some?->date_val?->format("Y-m-d") ?? "default";
// Test 3
echo ($some->date_val ?? null)?->format("Y-m-d") ?? "default";
// Test 4
echo ($some?->date_val ?? null)?->format("Y-m-d") ?? "default";
Resulted in this output:
// Test 1 - throws Fatal Error
// Test 2 - throws Fatal Error
// Test 3 - outputs "default" as expected
// Test 4 - outputs "default" as expected
But I expected this output instead:
All tests to output "default"
The alternative approach is:
class SomeClass {
public ?DateTime $date_val = null;
}
However, doing this "marks" SomeClass::$date_val as "possibly being null".
The expected use case for the original class:
A "valid"
SomeClassshould always have$datetyped asDateTimeinstead of being typed asnullORDateTime
We can potentially:
- set
$date_valto be protected - override the magic
__getmethod toreturn $this->{$name} ?? null;
This works outside the class, but not within it:
class SomeClass {
protected DateTime $date_val;
public function __get(string $name): mixed {
return $this->{$name} ?? null;
}
public function toArray(){
return [
"date_val" => $this->date_val?->format("Y-m-d") ?? "default",
];
}
}
$some = new SomeClass;
// Test 1 - this works and outputs "default"
echo $some->date_val?->format("Y-m-d") ?? "default";
// Test 2 - this throws a Fatal Error
var_dump($some->toArray());
The magic method __isset() is not called in any of the above cases where a Fatal Exception is thrown.
PHP Version
PHP 8.0.2
Operating System
Windows 10
Hi @iw-rylee!
Properties with no types are implicitly set to null when the object is instantiated. This is not the case for properties with a type. They are initialized to the internal IS_UNDEF value which throws errors when accessed. The nullsafe operator by design guards against null but not the internal IS_UNDEF value.
In general, all properties should be initialized after __construct. If they can't, it makes sense to make them nullable and set them to null. Marking them nullable makes users of the property aware that it might be null and that they might have to add a null-check of some sorts.
I think the biggest confusion here is that another operator interacting with null works:
($this->date_val ?? null)?->format("Y-m-d") ?? "default"
This doesn't throw the error, but it's labelled as "Null coalescing operator" ¶.
From that, I would think that something labelled "Nullsafe operator" ¶ works with values/properties in the same way.
It seems they have very similar naming but different underlying logic.
I understand that the documentation for "Null coalescing operator" explains that it's essentially a replacement for cases where isset() is used with a ternary operator.
There doesn't seem to be the same level of documentation (that I could find easily) that explains how the "Nullsafe operator" works.
For consistency, it seems sensible (to me at least) that either both throw an exception, or both work.
Is there a reason one guards against IS_UNDEF and the other doesn't?
I think the biggest confusion here is that another operator interacting with null works:
I agree that the naming clash of nullsafe and null coalescing is somewhat unfortunate. However, the decision of handling or not handling IS_UNDEF was very explicit for both and not something we can change at this point without and RFC.
Is there a reason one guards against IS_UNDEF and the other doesn't?
In my personal opinion, the null coalescing operator silences too many errors. E..g I think $doesNotExist ?? 'bar' should emit a undefined variable warning. The same for $foo->doesNotExist ?? 'bar'. On the other hand, that makes it more inconsistent (not behaving the same as isset()) and thus worse in other ways.