Unset static properties

  105204
April 10, 2019 12:20 benjamin.morel@gmail.com (Benjamin Morel)
Hi internals,

I came across this when playing with static properties:

    class A {
        public static int $x;
    }

    // This throws, as expected:
    // Error: Typed static property A::$x must not be accessed before
initialization
    echo A::$x;

    // Once we initialize the property:
    A::$x = 1;
    echo A::$x; // 1

    // There is no way to revert the property back to uninitialized state:
    // Error: Attempt to unset static property A::$x
    unset(A::$x);

Is this expected behaviour? I don't currently have a use case for this, but
am wondering whether it is consistent to disallow un-initializing a static
property when it's allowed to be uninitialized in the first place.

Cheers,
Ben
  105205
April 10, 2019 12:57 nikita.ppv@gmail.com (Nikita Popov)
On Wed, Apr 10, 2019 at 2:20 PM Benjamin Morel morel@gmail.com>
wrote:

> Hi internals, > > I came across this when playing with static properties: > > class A { > public static int $x; > } > > // This throws, as expected: > // Error: Typed static property A::$x must not be accessed before > initialization > echo A::$x; > > // Once we initialize the property: > A::$x = 1; > echo A::$x; // 1 > > // There is no way to revert the property back to uninitialized state: > // Error: Attempt to unset static property A::$x > unset(A::$x); > > Is this expected behaviour? I don't currently have a use case for this, but > am wondering whether it is consistent to disallow un-initializing a static > property when it's allowed to be uninitialized in the first place. >
We could add support for this, but as the general consensus is that we'd much rather remove support for unsetting declared object properties (if we could cover the lazy loading use case in a different way), I don't think it makes sense to allow this. Nikita
  105206
April 10, 2019 13:01 benjamin.morel@gmail.com (Benjamin Morel)
> We could add support for this, but as the general consensus is that we'd much rather remove support for unsetting declared object properties (if we
could cover the lazy loading use case in a different way), I don't think it makes sense to allow this. Ok, thanks. Just wanted to double check. Note that I'm actually liking the ability to unset declared properties for 2 reasons: - lazy loading, as you mentioned - ability to differentiate between unloaded properties, and properties loaded as null from the database (useful in data mappers) Ben