Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100972 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 78264 invoked from network); 27 Oct 2017 20:52:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Oct 2017 20:52:06 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 212.232.28.122 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 212.232.28.122 mx201.easyname.com Received: from [212.232.28.122] ([212.232.28.122:40834] helo=mx201.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5D/21-28573-07C93F95 for ; Fri, 27 Oct 2017 16:52:01 -0400 Received: from cable-81-173-135-10.netcologne.de ([81.173.135.10] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1e8Bbc-0001Tq-3F for internals@lists.php.net; Fri, 27 Oct 2017 20:51:53 +0000 Reply-To: internals@lists.php.net To: php-internals Message-ID: <93a05192-ed34-2164-50f9-2799899b32d1@fleshgrinder.com> Date: Fri, 27 Oct 2017 22:51:38 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="lHjclR1DBMwVQs1mtC9mpS6tKkblB9FpG" X-DNSBL-BARRACUDACENTRAL: YES X-DNSBL-PBLSPAMHAUS: YES Subject: Constants and Access Modifiers From: php@fleshgrinder.com (Fleshgrinder) --lHjclR1DBMwVQs1mtC9mpS6tKkblB9FpG Content-Type: multipart/mixed; boundary="r2IdpcPpMMPTlRklTWBNrAVTt1oAdF6lE"; protected-headers="v1" From: Fleshgrinder Reply-To: internals@lists.php.net To: php-internals Message-ID: <93a05192-ed34-2164-50f9-2799899b32d1@fleshgrinder.com> Subject: Constants and Access Modifiers --r2IdpcPpMMPTlRklTWBNrAVTt1oAdF6lE Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable 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 # 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. --=20 Richard "Fleshgrinder" Fussenegger --r2IdpcPpMMPTlRklTWBNrAVTt1oAdF6lE-- --lHjclR1DBMwVQs1mtC9mpS6tKkblB9FpG Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJZ85xjAAoJEOKkKcqFPVVr2DUP/07JThHg+KP6JSSTCw1Bjybm qw7MSbAQ/hcguTQ0AGiQa/abJDWd8rZDaa4frxQnqAnhB4//8Cn5TJokgCUrX3Tt 0nhu++HvKIJxoOKgG3nR+R/xdq/AIoJTOSzZpbeyum0sQSyAMioboU9MItSIPPma Tm+aFvOdToitqeCBKLlBpBREeFmBKcInbcYpF30jlhoFou2mGuKpN/8TDcCeksis f+ha9wa+ZEiFUGnwgtdOZGafOBGnLO6FSu7Dks1J6ZxWxTpLAqW8K+aa6vAts+Md dve7Vy53+zvr02YUhBFu+C8oST99/FP7r8Lw7BifyuQGLC4d9Ln1gPj1DwsadQ/5 /glQfyje0gtq+VZYqvOPGxAZcG8z7mYGr8UwF6zCX15EjJ7HIAiGTRLWFnTYkuF6 BkZWx/SO/00ySmJxNBnla+slaxxqjwWTXbdSa1RS8imkGMDOxSFXc0j6KKpoT0HH kCOTCub6gJr17lvhlJ8n436TqUe7X7+P3x1e+Cx+EfZGKtlAuRAEi0snqLeMQY7t jAfSqpkXUW4QTbHIDjHpZAA+uZT8mL4Z5GEWet/8X3TjpURaBHnLzk/1ezm09bx/ JmhlpbffE4t78nv3rhWRfgI+Gicsc6Fqozkid+5ziCkAebQCNNPl6GFcW9O4sOxU ZHLFiOudk1KE05dzGD5k =HjeY -----END PGP SIGNATURE----- --lHjclR1DBMwVQs1mtC9mpS6tKkblB9FpG--