The problem is a completely different one, how should the following code
behave?

  class A {

    public int $x;

  }

  (new A)->x;

The property has no value assigned but it is being accessed. The current
PHP behavior is to simply initialize it with null. But this is
impossible according to the type definition.

There are not many ways to handle this. I think we already had all of
them proposed:

0. Fatal error after __construct was called.
1. Fatal error and abort.
2. Initialize with appropriate type.
3. Initialize with null.

Option 0. is out the window because it creates endless edge cases.

Option 1. is extremely brutal and not necessarily what we want (lazy,
anyone?).

Option 2. has a huge problem with objects because it cannot initialize
e.g. a \Fleshgrinder\Custom\SuperClass nor a \DateTime.

Option 3. is the current behavior but silently doing so results in a
type hint violation. Emitting an E_NOTICE at this point is the most
sensible thing that we can do at this point in my opinion. Extending
this logic to all kind of properties is just logical to keep
conditionals in the internals low and have a consistent behavior across
all of the userland functionality. After all, aren't the following
things equal?

  $a;
  echo $a; // null + E_NOTICE

  class O {
    public int $x;
  }
  echo (new O)->x; // null + E_NOTICE

One could even argue that an E_NOTICE is required for void routines too.

  function f() {}
  echo f(); // null + E_NOTICE

But that's another story. :)

-- 
Richard "Fleshgrinder" Fussenegger

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to