On Wed, Feb 19, 2020, at 1:36 PM, Marco Pivetta wrote:
> On Wed, Feb 19, 2020, 19:51 Larry Garfield <la...@garfieldtech.com> wrote:
> 
> > On Wed, Feb 19, 2020, at 11:05 AM, Máté Kocsis wrote:
> > > Hi Internals,
> > >
> > > I'd like to move my RFC forward to the discussion phase:
> > > https://wiki.php.net/rfc/write_once_properties
> > >
> > > In short, I propose to add support for a new property modifier that would
> > > allow properties to be initialized, but not modified afterwards.
> > >
> > > Cheers,
> > > Máté Kocsis
> >
> > As envisoned, does this allow for a property to be set to a dynamic
> > value?  My concern is that while a public locked/writeonce property is
> > great for access, it doesn't do anything to enable lazy setting on first
> > access.  In fact the only way to do that would be to make it private and
> > wrap access in a method, which would look exactly like that does now but
> > with an extra keyword that doesn't actually offer much.
> >
> > You could set the value in advance in the constructor, but then it's not
> > lazy, just locked.
> >
> > Is there a way it could support lazy-on-first-use then locked?
> 
> 
> Máté did run a few teats: operating on a lazy value (before initialisation)
> seems to work as expected 👍

Erm.  I don't think I was clear in my intent.  I would expect that operating on 
one of these properties before it's initialized will throw an error:

class Foo {
  readonly public string $bar;
}

$f = new Foo();
print $f->bar; // This should fail.

My question is whether this will work:

class Foo {
  readonly public string $bar = $this->value();

  private function value(): string { return "hello"; }
}

$f = new Foo();
print $f->bar; // I would want this print "hello".

Does that work currently or no?  If so, this is pretty sweet.  If not, it seems 
to be of limited use.

--Larry Garfield

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to