Re: RFC - Immutable classes

From: Date: Mon, 08 Aug 2016 12:47:13 +0000
Subject: Re: RFC - Immutable classes
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-94922@lists.php.net to get a copy of this message
Sure my final properties was first thought, lately in discuccion it turns into immutable, sealed and final see here https://marc.info/?l=php-internals&m=146005058530881&w=2 Have you also read about Immutable modifier from last year? http://marc.info/?t=144766539400001&r=1&w=2 it's exactly about immutable classes! 2016-08-08 14:46 GMT+02:00 Silvio Marijić <marijic.silvio@gmail.com>: > Btw, I was not aware that there was already conversation on immutable > classes. > > 2016-08-08 14:31 GMT+02:00 Silvio Marijić <marijic.silvio@gmail.com>: > >> I see your standpoint, but I think it could cause confusion because of >> final classes. I think "immutable" is more suited here. >> What I try to achieve is something similar like case classes in Scala. >> >> Best regards, >> >> 2016-08-08 13:16 GMT+02:00 Michał Brzuchalski <michal@brzuchalski.com>: >> >>> Hi Silvio, >>> >>> Look into my talk about final properties https://marc.info/?õLû >>> 10E2£¦Sž$_8 >>> t=145979255800003&r=1&w=2 maybe there would be something usefull. >>> There is also talk about mutable and immutable properties and other >>> class modifiers also about var and val. >>> I'm not sure that it should be class midifier rather than proprty >>> modifier keyword. >>> >>> regards, >>> -- >>> Michał Brzuchalski >>> >>> 2016-08-08 12:31 GMT+02:00 Silvio Marijić <marijic.silvio@gmail.com>: >>> >>>> Hi, >>>> >>>> I would need your help with one idea. I'm working on one RFC that I'm >>>> would >>>> like to submit. Idea is that after you initialize object eg. after >>>> constructor returns, object would be locked, and you wouldn't be able to >>>> change properties on that object anymore. It would like this: >>>> >>>> <?php >>>> >>>> immutable class Email { >>>> > public $email; >>>> > public function __construct($email){ >>>> > $this->email = $email; >>>> > } >>>> > } >>>> > $email = new Email("example@email.com"); >>>> >>>> >>>> >>>> After instance of class is created, object is "frozen" so call like this >>>> >>>> $email->email = "new@email.com"; >>>> >>>> >>>> Would result in error. >>>> >>>> I have already implementation up to certain degree, but I need one >>>> advice >>>> from more experienced developers. Where is the place where I could put >>>> logic to lock object after the constructor has finished? Maybe in zend >>>> vm >>>> on ZEND_NEW token? >>>> >>>> Some constraints are needed: >>>> >>>> 1. Child class that extends immutable class must be defined as >>>> immutable >>>> also. >>>> 2. If property on immutable class contains object, it must be >>>> instance >>>> of immutable class. >>>> 3. You can not have immutability per property, it either whole class >>>> or >>>> none. >>>> >>>> Thank you all in advance. >>>> >>>> -- >>>> Silvio Marijić >>>> Software Engineer >>>> 2e Systems >>>> >>> >>> >>> >>> -- >>> pozdrawiam >>> -- >>> Michał Brzuchalski >>> >> >> >> >> -- >> Silvio Marijić >> Software Engineer >> 2e Systems >> > > > > -- > Silvio Marijić > Software Engineer > 2e Systems > -- pozdrawiam -- Michał Brzuchalski

« previous php.internals (#94922) next »