Re: [RFC] New in initializers

From: Date: Sat, 20 Mar 2021 18:58:21 +0000
Subject: Re: [RFC] New in initializers
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-113642@lists.php.net to get a copy of this message


> Le 19 mars 2021 à 12:22, Nikita Popov <nikita.ppv@gmail.com <mailto:nikita.ppv@gmail.com>> a écrit :
> 
> I've updated the RFC (and implementation) to evaluate class constants and
> static properties at time of class declaration. As such, everything should
> have a well-defined evaluation order now.
> 
> However, this also means that this RFC now also contains a
> backwards-compatibility break: Anything used inside class constant / static
> property initializers needs to actually be available at the time the class
> is declared. You can't first declare the class, then declare some
> additional constants it uses, and then use it.
> 

I don’t like having the static property initialisers and constants evaluated eagerly. I typically
declare the main class followed by zero or more class helpers in the same file: with the proposed
semantics, I may be forced to occasionally reorder my code. One particular case I have in mind is
the following refactoring that I may perform with the advent of enums:

```php
class Foo {

    const STATE_OPEN = 1;
    const STATE_CLOSE = 2;

    function setState(int $state) {
         // ....
    }
}
```

into:

```php
class Foo {

    #[\Deprecated] const STATE_OPEN = FooState::OPEN;
    #[\Deprecated] const STATE_CLOSE = FooState::CLOSE;

    function setState(FooState $state) {
         // ....
    }
}

enum FooState {
    case OPEN;
    case CLOSE;
}
```

—Claude




Thread (38 messages)

« previous php.internals (#113642) next »