php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Nullsafe Operator doesn't work with uninitialized properties

Open iw-rylee opened this issue 4 years ago • 3 comments

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" SomeClass should always have $date typed as DateTime instead of being typed as null OR DateTime

We can potentially:

  • set $date_val to be protected
  • override the magic __get method to return $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

iw-rylee avatar Mar 16 '22 06:03 iw-rylee

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.

iluuu1994 avatar Mar 16 '22 08:03 iluuu1994

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?

iw-rylee avatar Mar 17 '22 00:03 iw-rylee

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.

iluuu1994 avatar Mar 17 '22 10:03 iluuu1994