Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92123 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 96897 invoked from network); 7 Apr 2016 13:25:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Apr 2016 13:25:31 -0000 Authentication-Results: pb1.pair.com smtp.mail=t.carnage@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=t.carnage@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.52 as permitted sender) X-PHP-List-Original-Sender: t.carnage@gmail.com X-Host-Fingerprint: 209.85.215.52 mail-lf0-f52.google.com Received: from [209.85.215.52] ([209.85.215.52:35259] helo=mail-lf0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 85/5A-48788-9CF56075 for ; Thu, 07 Apr 2016 09:25:30 -0400 Received: by mail-lf0-f52.google.com with SMTP id c126so56985792lfb.2 for ; Thu, 07 Apr 2016 06:25:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=25y3032zl/h9NKXa4DOeIUcytN/+pgLp5cjhuIMVc0Y=; b=J7MLM0b3upmsuVec/wAvlp46KgiCIqOkhFDfFeuPDtGzmKKJdRlPtqB2Qp19MGIvXx gUw8zjDDcslMA4S+5YgHbL6qIDIxtBhW3tAPiexYZNrUNZ/w4J4PTvGS0EOMMIfBvaC1 DIiA4TPSlMfuV66b1/Cr0FEEw0bDbZwNn28WlwpFtBKK4zjmgYLIQflrP/xWrMB7i+sn HP9jkhilfvfXJuD9osc+Ldyq0QqMhS+K+eVsCVZPU7bOA81ePAjsTRB9ytARmwHfdqZV rBUteGxPwsdXJiCv5CkBFCPIpPc8PhT1rineTIQHUF6ge5/ON+LIaRNtmmY6QEyXDC+c pVZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=25y3032zl/h9NKXa4DOeIUcytN/+pgLp5cjhuIMVc0Y=; b=QiGY7HhoGyBxl2WB8YnXJy5IRUXI5Nt7BnTF/7x0xb6sb2pwn8lyWEQ1xHHjeyrO5R QPUBWEUUBS019x/oTvCtycyViyjUEKPrVMN3gWH4aICKnwyI3b/woV4KBuDzh/nqZJuL 18oHdDwV4XzK1j47Zg/VuMLYkccvBNYDkzegkEqIe2DuJ8XaR1HtRABh/VfHd/vKVwnB itXonJGqfDfN5AALh9DK8TREevzcSrrYx7nGY7Vx/1WNUxHVKuHAXszCd+uFUSSRPhYi R9rp6hYUSaOuX0hMCxl3UVteJ5ptLJylr9N/3V+nzKvCBcvgtN1f3c2wKP0iGr0BRoXU nh2A== X-Gm-Message-State: AD7BkJIUgTKan+aSWwQbvsoTvjcrLKvTykYBnDP2vMYJ6PYfFmuE0CuyhqJNymioGM3Slrn3JtkcgB8lgBQIVQ== MIME-Version: 1.0 X-Received: by 10.25.37.136 with SMTP id l130mr11768474lfl.153.1459873700757; Tue, 05 Apr 2016 09:28:20 -0700 (PDT) Received: by 10.112.0.200 with HTTP; Tue, 5 Apr 2016 09:28:20 -0700 (PDT) In-Reply-To: References: Date: Tue, 5 Apr 2016 17:28:20 +0100 Message-ID: To: =?UTF-8?Q?Micha=C5=82_Brzuchalski?= Cc: Marco Pivetta , PHP Internals List Content-Type: multipart/alternative; boundary=001a113fcdae59c8ba052fbf552e Subject: Re: [PHP-DEV] Final properties From: t.carnage@gmail.com (Chris Riley) --001a113fcdae59c8ba052fbf552e Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 5 April 2016 at 11:57, Micha=C5=82 Brzuchalski = wrote: > Hi, > > 2016-04-05 12:13 GMT+02:00 Marco Pivetta : > > > Hi, > > > > On 5 April 2016 at 12:06, Micha=C5=82 Brzuchalski > > 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 > >> > > > > > It works with public, private and protected properties. > > > > 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 change= d > >> 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. > > > > Wiki about Final in Java > https://www.wikiwand.com/en/Final_(Java)#/Final_variables says: > > # Final variables > A final variable can only be initialized once, either via an initializer = or > an assignment statement. It does not need to be initialized at the point = of > declaration: this is called a "blank final" variable. A blank final > instance variable of a class must be definitely assigned in every > constructor of the class in which it is declared; similarly, a blank fina= l > static variable must be definitely assigned in a static initializer of th= e > class in which it is declared; otherwise, a compile-time error occurs in > both cases.[6] (Note: If the variable is a reference, this means that the > variable cannot be re-bound to reference another object. But the object > that it references is still mutable, if it was originally mutable.) > > I took the name from Java and only implemented behavior like that, thats = wy > I used `final`. > > > >> 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=C5=82 Brzuchalski (aka brzuchal) > >> > >> 2016-04-05 11:13 GMT+02:00 Marco Pivetta : > >> > >>> Hi Micha=C5=82, > >>> > >>> 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 alterin= g > >>> 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=C5=82 Brzuchalski > >>> wrote: > >>> > >>>> Hey Internals, > >>>> > >>>> I'm new here. I've been wondering / working on adding final properti= es > >>>> 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 et= c. > >>>> impl > >>>> like: > >>>> > >>>> class Money { > >>>> public final $amount; > >>>> public final $currency; > >>>> public function __constructor($amount, $currency) { > >>>> $this->amount =3D $amount; > >>>> $this->currency =3D $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=C5=82 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`, b= ut > > that may change in future, so better factor it into any possible outcom= es > > of the RFC. > > > > I don't know if it works with typed properties. > I can provide an RFC but need Wiki Karma (or smth like that) for my accou= nt > (login: brzuchal) and don't know how to receive it. > > > > Cheers, > > > > Marco Pivetta > > > > http://twitter.com/Ocramius > > > > http://ocramius.github.com/ > > > > Cheers, > -- > Micha=C5=82 Brzuchalski (aka brzuchal) > I started a discussion on this a while back, but it fizzled due to not having an elegant solution for the 'give me a new one of these with these values changed' problem. I would HIGHLY suggest not using the final keyword. If final is used as a keyword I would expect it to mean that this property definition cannot be changed in a child class. See: https://marc.info/?l=3Dphp-internals&m=3D144766539202647&w=3D2 Would be hap= py to pick this up again and help get the RFC created. --001a113fcdae59c8ba052fbf552e--