Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99679 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 17977 invoked from network); 29 Jun 2017 10:14:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Jun 2017 10:14:43 -0000 Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.49 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.49 mail-wm0-f49.google.com Received: from [74.125.82.49] ([74.125.82.49:35256] helo=mail-wm0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F3/6B-07609-013D4595 for ; Thu, 29 Jun 2017 06:14:41 -0400 Received: by mail-wm0-f49.google.com with SMTP id w126so77407814wme.0 for ; Thu, 29 Jun 2017 03:14:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=6LGHcb6RWykiguJCFDUuiSwZ8QC+CNmxX9Yv80fKVZg=; b=ksoAS2GGpJUJ3jgWh2SXHkYM1Kqv7ZY0o1AbQ+1O2nTfoe+2efCjyd6LZGdkwrAvLl qYBZ6kTRwZJ/Kj3Hfxnn2s4Z0S5/d+VO8H+Ibfhhe5t2+FzUmay9++YpW3j8XH5+g9vt uBIw69LAV1ZIRGBWB5O86qUtYMA4bjIxSWApqGxoUKyHLTI/YT05yPS+9NDb57un/MKA cNOAYe5fllsqmDTPEcGpD5axjb4SrzpDUYL0pSEYFeKIc1uuQPvW0wX4+5fS8SNKtyIL rTBKxxpNXhPNFJIGF0HYcreo2qTjrztp948DFng4ojFIYK4aa4CQDo6YRoJm+raFvjiU 3lgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=6LGHcb6RWykiguJCFDUuiSwZ8QC+CNmxX9Yv80fKVZg=; b=Z0AS8LpTZl+CMebEqfdsm/WkJdXZ69xF3g4ALaum8Mg510hPyiOPERu0jM0Z27JZW7 WUnzUGcsqeguN3CqOm0RZ94stPaoaTeIyXSb/ozmLdvSgAOGyz3ZpXgKsDS8lLNlaWJj KJdjDWgvAOWbHlyo6xzrsvzm+AZe1F8GYocE21Ti/MWuWlHsXjIC0//vvc2klfMCM1pR 9tybZegTNFUrp8YStU1LPWEGD1I2t5IBAiSIEnyV5c4nwPpjfP6jeLHwPkl1BuewIplO CtYHRk8n0d6y+4X6IFXUCJ4aVqtKMXFlYcc642xb3MONgIO3u7K+/ZxwetwYacxy9pT5 gnCQ== X-Gm-Message-State: AKS2vOxB3uXapMzHcusZ1vezQJO6qJIMQTpH9cdz6CzWNowX6I6eTT2e f/T+BeX8oH0x8oTu9Oqpu4M/ZjN7KA== X-Received: by 10.28.140.194 with SMTP id o185mr1379260wmd.87.1498731277746; Thu, 29 Jun 2017 03:14:37 -0700 (PDT) MIME-Version: 1.0 Received: by 10.223.161.155 with HTTP; Thu, 29 Jun 2017 03:14:17 -0700 (PDT) In-Reply-To: References: <54e9e9f4-1fda-d7db-61a4-2a1574005a79@rhsoft.net> Date: Thu, 29 Jun 2017 12:14:17 +0200 Message-ID: To: David Rodrigues Cc: "lists@rhsoft.net" , PHP Internals List Content-Type: multipart/alternative; boundary="001a114694b66c92df0553169100" Subject: Re: [PHP-DEV] Final variables From: ocramius@gmail.com (Marco Pivetta) --001a114694b66c92df0553169100 Content-Type: text/plain; charset="UTF-8" Hi David, On Thu, Jun 29, 2017 at 11:58 AM, David Rodrigues wrote: > > The `final` modifier would likely clash with property type definitions, > if those ever make it to the language. > > Still, a few pain-points remain: > > * References to properties (you cannot reference final properties > anymore, and that is effectively a BC break for anything using reflection > and closure scope binding) > > I don't know if I understand, but for properties you could redefine a > final property on constructor (as Java). https://pastebin.com/bTZcUT33 > No, the problem is following: class Foo { final public $bar = 'baz'; } $foo = new Foo; $bar = & $foo->bar; $bar = 'taz'; var_dump($foo->bar); This kind of ugly code will break. > * Arrays and resources as properties > > You can modify arrays or resources on final variables, since that you > don't modify the reference. https://pastebin.com/D38wL8x7 > That doesn't make it "final", although I get your point, and yes, it basically prevents decreasing the reference count on that particular property. I think this kills the usefulness of the feature, but it's indeed still useful. > > * Named (Factory) constructors > > I don't understand. > Relatively common pattern: class Foo { private final $bar; private function __construct() { /* hammertime */ } public static function create($something) : self { $instance = new self(); $instance->bar = $something; return $instance; } } See also http://verraes.net/2014/06/named-constructors-in-php/ > > * Property nullability - how do you define if a property was written to > the "last valid time" before freezing it > > Because PHP don't have support to "variable initialization" (eg. "$name;") > then you should only initializate it when you will write the initial (and > freezed) value. https://pastebin.com/Ua6DFUC1 > Got it, so a "first write" would be equivalent to a freeze operation? When do you seal the property if the value isn't overwritten? Would you disable default values for properties that are declared `final`? > > > * Unset properties and providing an alternative mechanism to > distinguish "uninitiated", "undefined" and "frozen" in he event of final > properties usage, without breaking current property access semantics > > It will be modified. Final variables still are a variable, but with the > status that "never change after initializate" (a flag). We can just > implement a new reflection method to properties to identify if it > isFinal(), or even for variables like "is_final($var)". By other side, this > last doesn't make sense because the final variable is just a "code > documentation" to the own dev knows that is not allowed modify it anymore. > Maybe we need implements that only for ReflectionProperty (first case), > because it could be part of a code that the user have not modify access > (eg. vendor code). https://pastebin.com/jSjNQACd > The reflection API is a given, but the problem is still the same: class Foo { private final $foo; private $lazyLoad; public function saySomething() { return 'Saying ' . $this->foo; } private function __construct() { } public static function new() : self { $instance = new self(); $instance->foo = 'something'; return $instance; } public static function newLazy() : self { $instance = new self(); $instance->lazyLoad = function () use ($instance) { $instance->foo = 'something lazy'; }; unset($instance->foo); return $instance; } public function __get($name) { ($this->lazyLoad)(); return $this->foo; } } $foo = Foo::new(); var_dump($foo->saySomething()); $lazyFoo = Foo::newLazy(); var_dump($lazyFoo->saySomething()); https://3v4l.org/UPVWF Hope that clarifies it. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --001a114694b66c92df0553169100--