On Thu, Mar 4, 2021 at 6:21 AM Michał Marcin Brzuchalski < michal.brzuchal...@gmail.com> wrote:
> Hi Nikita, > > śr., 3 mar 2021, 16:04 użytkownik Nikita Popov <nikita....@gmail.com> > napisał: > >> Hi internals, >> >> I would like to propose allowing the use of "new" inside various >> initializer expressions: https://wiki.php.net/rfc/new_in_initializers >> >> In particular, this allows specifying object default values for properties >> and parameters, and allows the use of objects as attribute arguments. >> >> The RFC is narrow in scope in that it only adds support for "new". An >> extension to other call kinds should be straightforward though. >> > > The reflection mechanism for properties, constants and parameters is not > described in RFC but I do believe it should for constants part. Correct me > if I'm wrong there. > > The reflection for ReflectionProperty::getDefaultValue the same for > ReflectionParameter is similar and I get that it possibly should evaluate > the value on each call. But what about constants which by their nature > don't change? > Does that mean that each time ReflectionClass::getConstant is called the > value is already evaluated and the return value always refer to the same > object instance. Is that right? I guess it is. > > IMO it deserves to be described in the RFC. > > In general I love the proposal was especially thinking of it for static > variables. > > Cheers, > Michał Marcin Brzuchalski > I've added a section that describes reflection methods. It works exactly as you say. There is one interesting case though: How should ReflectionObject::newInstanceWithoutConstructor() work? In the current implementation, it will still evaluate the property defaults, including new expressions. This makes sense to me, but I can also see an argument that this method should not evaluate them -- however, in that case I believe it should not initialize any properties at all (leave them in "uninitialized" state). Populating property default values depending on what kind of expression they contain seems like a no-go to me. @Ben Ramsey: Yes, it's possible to use nested new. Generally, all supported constant expressions can be freely combined. Regards, Nikita