Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100974 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 81530 invoked from network); 27 Oct 2017 21:15:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Oct 2017 21:15:44 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.170 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.223.170 mail-io0-f170.google.com Received: from [209.85.223.170] ([209.85.223.170:50616] helo=mail-io0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D1/D1-28573-FF1A3F95 for ; Fri, 27 Oct 2017 17:15:43 -0400 Received: by mail-io0-f170.google.com with SMTP id 97so15448291iok.7 for ; Fri, 27 Oct 2017 14:15:43 -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; bh=9fFH5xxLrZXxrjKSdm3k8KPlKAI72hvFFHocRTSN3Vc=; b=RR/Gml6l67F2E9LfyQAyyHFxkLiq813IMG4I73kF+aqdjjQI16AQcyLUneoAaQGqxV QFKIc1JZv8fG5hon9AUR1YXUUUrlzXTG6HZTbwrGxOwRCfxIUFzx21QYPFj/dimxujz+ SUZ+6wwb4wqQMVUhBJzmSc9L4am6OVQRv+sBQE0aqC+47pejw38s2mR4XiwkuLfxoEFR fIRLnfqjPP7CdRpCK9IsAvTEN/gffWkYygLSluvoF7OjusxpUW8Uhb/QJFPHOruxcgie wPiyVMIiBRH80Dxa2oWVXbpv31Q123UgxCJsGxY4azQM7tH01Y3c7jJ7T+JHHtVNTzSc g2wA== 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; bh=9fFH5xxLrZXxrjKSdm3k8KPlKAI72hvFFHocRTSN3Vc=; b=BvupRtJqc58GtjU9RQ0DW7WtBXI80AQlC1UDkdpJw/Rnzor1R3U5nX7gsXLjiSAoOs BYaZwELi8CQkeYLVAdnU/NGhytPGqO+vcPFrapo54z845+t9n2FDat7oK6KdGT/cF7LO SqDC7gYapnr6quP0O/NM0EpWIy3p1CMhy607JgReRHL7oux/znMNrHab8m3SP4gJyc/O NWLghNpuzJQI6gtphJM6LnYfgBQtYxrtZ0Bt16qDVI+aORZf7dseMl17w683TwJzSqR8 g6JWyybPEzhrvzltQeW+OXn2ur+6KLNfJbClD0zk1HU/EGuNvGMvPObECyP75R/UFLwM qO5Q== X-Gm-Message-State: AMCzsaX+655rg3jCSsf2H1Yhp/sngV+Hzsj5F2VrtEQOv808nj/rAOr9 vDKdMOHYR+ff0dX7NrPSjhBoEKkG+129+H7EhBw= X-Google-Smtp-Source: ABhQp+QI+E3aedRsj+oe/fLqYaw1534rwhYCcRNdVFQ5417T7qB6fbu3Ebj93yY3nZE/Rvw96wxl9fPXw1h5JnRoQ74= X-Received: by 10.107.9.210 with SMTP id 79mr2185184ioj.94.1509138940068; Fri, 27 Oct 2017 14:15:40 -0700 (PDT) MIME-Version: 1.0 Received: by 10.107.35.78 with HTTP; Fri, 27 Oct 2017 14:15:39 -0700 (PDT) In-Reply-To: <93a05192-ed34-2164-50f9-2799899b32d1@fleshgrinder.com> References: <93a05192-ed34-2164-50f9-2799899b32d1@fleshgrinder.com> Date: Fri, 27 Oct 2017 23:15:39 +0200 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="001a113ec2ca708a6f055c8dcaa7" Subject: Re: [PHP-DEV] Constants and Access Modifiers From: nikita.ppv@gmail.com (Nikita Popov) --001a113ec2ca708a6f055c8dcaa7 Content-Type: text/plain; charset="UTF-8" On Fri, Oct 27, 2017 at 10:51 PM, Fleshgrinder wrote: > Hey Internals! > > We currently have a couple of things that are broken about constants, > and I wanted to gauge if people are interested in a) fixing it as well > as b) to get to know the root causes of these things. > > # 1 > > A constant defined in an interface cannot be overwritten in the class > that implements the interface, however, further subclasses can overwrite > the content of the constant at will. > > - https://3v4l.org/DFB37 > - https://3v4l.org/9LMci > > A constant defined in a class can be overwritten by any subclass at will. > > # 2 > > Constants can reference themselves, and subclasses can define the actual > value. Kind of like abstract constants. This works nicely while working > with an actual instance of the object, however, it breaks in the moment > that constant is accessed anywhere in the parent, or from any other > constant. > > - https://3v4l.org/HUCTh > - https://3v4l.org/5aYB5 PHP does not permit self-referencing constants. However, this is only checked when the constant is first accessed. In your first example the constant is never accessed, so no error is thrown. This has nothing to do with subclasses defining the value -- you're using late static binding, so you're accessing the constant of the child class directly. PHP cannot detect self-referencing constants during compilation, because they may be formed through non-trivial cycles involving multiple constants, across multiple files. Nikita # 3 > > A constant that is defined with a visibility in a parent class, cannot > be references between subclasses, like it is possible with methods. > Instead we are presented with an access violation. > > - https://3v4l.org/2lU3i > > Note that this behavior is the same for static and instance properties > that are being redefined in a child class. Hence, access is defined by > location and not by modifier. > > # What I think > > I haven't thought very long about everything, but here are my initial > thoughts: > > ## 1 > > This issue could be resolved by changing the behavior to enable > overwriting of parent constant in any sublcass. This gives us a > consistent behavior. > > Afterwards we should add support for the final modifier, so that people > can seal their constants and protect them from redefinition. > > > Why not seal by default? > > It would disallow some dynamic programming that is possible with the > late static binding of PHP, and I honestly see no reason why we should > disallow something that is already possible: BC! > > ## 2 > > Disallow self-referencing constants in any context, and instead add > support for the abstract keyword to constants. This raises the question > on how to deal with constants in interfaces. I would allow the > definition of both constants with and without a value there. Those > without are abstract, those with a value are like they are right now. > Directly referencing an abstract constant should result in an error. > > Abstract constants are a great thing in combination with late static > binding, and the engine ensures that the value does not change over the > course of the runtime of the program. An attribute that is impossible > for methods. Dart for instance has support for the const keyword for > many elements, including methods. > > ## 3 > > This seems like a bigger construction site. I think that the behavior > should be that the access is determined by the modifier, and not the > location. Especially because doing anything else is to great of a BC. > The current behavior of allowing access to protected members of other > classes with the same parent also allows the creation of friend classes. > Although without the control that real friend classes have. > > -- > Richard "Fleshgrinder" Fussenegger > > --001a113ec2ca708a6f055c8dcaa7--