Re: Final properties

From: Date: Tue, 05 Apr 2016 10:13:48 +0000
Subject: Re: Final properties
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-92094@lists.php.net to get a copy of this message
Hi, On 5 April 2016 at 12:06, Michał Brzuchalski <michal@brzuchalski.com> wrote: > Hi Marco, > > Ad. 1 it is posiible to redeclare in a sub-class final property as > non-final, here is some gist presenting my current impl working like: > > https://gist.github.com/brzuchal/12ebda1efed59440a78ba43bff116728 > Does this work for public properties as well? > Ad. 2. final means class variable (like static) or class > instance > property can not change their reference, it is impossible to replace zval > in such property, in my opinion immutable is object behavior > when using > some methods you receive newly created (cloned) object which is changed > exactly like DateTimeImmutable > http://php.net/manual/en/datetimeimmutable.add.php > I am aware of what final means in other languages, it just seems that everyone then needs to translate back and forth from final to immutable. In addition to that, final has different meaning in inheritance (re-used keyword), so this adds to the confusion for people unfamiliar with the feature. > Ad. 3 it would be awesome if there could be final variables in general, it > would be quite stable if no one could change your variable, AFAIK zvals > have IMMUTABLE flag which could be used, don't know it for sure I'm rather > PHP dev than C there would be need some internals guru to ask if it's > posiible. > > P.S. We've meet on PHPCon'15 in Poland, thanks for +1. > YW! > > Cheers, > -- > Michał Brzuchalski (aka brzuchal) > > 2016-04-05 11:13 GMT+02:00 Marco Pivetta <ocramius@gmail.com>: > >> Hi Michał, >> >> First of all: +1 to this: very useful for value objects! >> >> A few questions: >> >> * can you re-declare a final property in a sub-class, making it >> therefore non-final? (I have reasons to do that, related with altering >> states via mappers) >> * do we want to use final, or >> immutable for these properties? >> final seems to just be a confusing term here. >> * is this feature portable to variables in general? I realize that >> adding metadata to ZVals has huge performance implications, but it would be >> interesting to do that for performance improvements further down the line >> >> Cheers, >> >> >> Marco Pivetta >> >> http://twitter.com/Ocramius >> >> http://ocramius.github.com/ >> >> On 4 April 2016 at 19:53, Michał Brzuchalski <michal@brzuchalski.com> >> wrote: >> >>> Hey Internals, >>> >>> I'm new here. I've been wondering / working on adding final properties >>> into >>> PHP lang. >>> >>> This work started once I had the time to read the "Core Java for >>> Impateient" by Cay S. Horstmann >>> and found it very usefull to have final properties like Java. >>> >>> Those properties differ than const because they can be >>> set at runtime - >>> but only once in their lifetime. >>> Such properties could be very usefull in Singletons, ValueObjects etc. >>> impl >>> like: >>> >>> class Money { >>> public final $amount; >>> public final $currency; >>> public function __constructor($amount, $currency) { >>> $this->amount = $amount; >>> $this->currency = $currency; >>> } >>> } >>> >>> In above example there is even no need for getter because those >>> properties >>> are immutable through >>> the final keyword, it means those properties cannot change their >>> references >>> just like in Java >>> https://en.wikipedia.org/wiki/Final_(Java) >>> >>> I've already started some impl on own fork >>> >>> https://github.com/php/php-src/compare/master...brzuchal:final-properties >>> I've got some basics in C programming, don't know yet if I can impl it >>> complex. >>> >>> I was wondering if it is usefull in yours opinion or is it only my >>> impression. >>> >>> I can provide an RFC if it sounds usefull and if I get Wiki karma >>> >>> Thanks >>> -- >>> Michał Brzuchalski (aka brzuchal) >>> >> >> > As an additional question: how will this (eventually) play with typed properties? I am aware that you cannot declare a class named final, but that may change in future, so better factor it into any possible outcomes of the RFC. Cheers, Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/

« previous php.internals (#92094) next »