Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92148 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 53862 invoked from network); 7 Apr 2016 19:39:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Apr 2016 19:39:02 -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.124 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 212.232.28.124 mx203.easyname.com Received: from [212.232.28.124] ([212.232.28.124:40916] helo=mx203.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 36/55-48788-357B6075 for ; Thu, 07 Apr 2016 15:39:00 -0400 Received: from cable-81-173-133-226.netcologne.de ([81.173.133.226] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1aoFlY-00041Z-Re for internals@lists.php.net; Thu, 07 Apr 2016 19:38:57 +0000 Reply-To: internals@lists.php.net References: <56ED495C.80809@fleshgrinder.com> <57069A72.7030100@fleshgrinder.com> <5706A182.6080401@gmail.com> <5706AA50.6080207@fleshgrinder.com> To: internals@lists.php.net Message-ID: <5706B73B.7010504@fleshgrinder.com> Date: Thu, 7 Apr 2016 21:38:35 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 MIME-Version: 1.0 In-Reply-To: <5706AA50.6080207@fleshgrinder.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="nIQuhgDFDFWb8nGpDe2X6jqH226XwLkKb" X-ACL-Warn: X-DNSBL-BARRACUDACENTRAL Subject: Re: [PHP-DEV] Access and Visibility Modifiers From: php@fleshgrinder.com (Fleshgrinder) --nIQuhgDFDFWb8nGpDe2X6jqH226XwLkKb Content-Type: multipart/mixed; boundary="iLkoO7DLBd9Wt21uPiS0F23MI9RAsiLeV" From: Fleshgrinder Reply-To: internals@lists.php.net To: internals@lists.php.net Message-ID: <5706B73B.7010504@fleshgrinder.com> Subject: Re: [PHP-DEV] Access and Visibility Modifiers References: <56ED495C.80809@fleshgrinder.com> <57069A72.7030100@fleshgrinder.com> <5706A182.6080401@gmail.com> <5706AA50.6080207@fleshgrinder.com> In-Reply-To: <5706AA50.6080207@fleshgrinder.com> --iLkoO7DLBd9Wt21uPiS0F23MI9RAsiLeV Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 4/7/2016 8:43 PM, Fleshgrinder wrote: > On 4/7/2016 8:05 PM, Rowan Collins wrote: >> Meanwhile, it might be worth checking the list archives for previous >> discussions of package visibility; I know it has come up a couple of >> times recently, in various forms, so it would be good not to repeat th= e >> same points again and again. >> >=20 > I check what I can dig up. :) >=20 Here we go: # 2007-12: Namespace access modifiers http://marc.info/?t=3D119750415000002 # 2009-09: New access modifier http://marc.info/?l=3Dphp-internals&m=3D125341032112627 Some nice keyword suggestions in this one: - group - trusted - party # 2011-03: Class Access Modifiers http://marc.info/?t=3D129917302700001 # 2013-02: I would like to write an RFC for the addition of an internal keyword http://marc.info/?t=3D136194893000003 # 2014-12: Package private class http://marc.info/?t=3D141929489300003 # 2015-11: Internal methods proposal http://marc.info/?t=3D144836744000001 I am sure that I overlooked something since the search turned up a lot of stuff. However, the above list already illustrates that such a feature is often requested and useful. This is further illustrated by other languages providing similar functionality. There were various questions raised in the threads above that I would like and try to answer here. > Can child classes redeclare visibility or override it. Yes, every class in an inheritance change can open up the visibility as is already the case for methods in PHP. The reason for that is simple: namespace Process\Pipes { public interface Pipes {} abstract private class AbstractPipes implements Pipes {} protected final class UnixPipes extends AbstractPipes {} protected final class WinPipes extends AbstractPipes {} } The UnixPipes and WinPipes classes are useless if they cannot open up the visibility because they cannot be instantiated from nowhere. > It would still be possible to get an instance of a private class from > the outside, e.g.: > > namespace Internals { > private class Foo {} > public class Bar {} > public function getFoo() { > return new Foo(); > } > } > } Yes and this is intentional. The class visibility modifiers only restrict the creation of such objects and not the interaction with them at runtime. Otherwise it would be impossible to implement a public interface in a private class that is being returned to the client. public interface Entities extends \IteratorAggregate {} public interface EntityManager extends Entities {} private class InMemoryEntities implements EntityManager {} private class FilesystemEntities implements EntityManager {} private class MySqlEntities implements EntityManager {} private class PdoEntities implements EntityManager {} switch (ENV) { case PROD: $entities =3D new MySqlEntities; break; case TEST: $entities =3D new InMemoryEntities; break; } $dic->addService('entities', $entities, [ 'alias' =3D> 'entitye_manager', ]); This is not /good code/ or anything but should illustrate the usefulness of it. In other words: It supports the I in SOLID. :) https://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 --=20 Richard "Fleshgrinder" Fussenegger --iLkoO7DLBd9Wt21uPiS0F23MI9RAsiLeV-- --nIQuhgDFDFWb8nGpDe2X6jqH226XwLkKb 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 iQIcBAEBCAAGBQJXBrc/AAoJEOKkKcqFPVVrR70P/1vOc2WZPg0FkDl/iPYOteRY canK6dn2S8gnIfpRUG3+JFUWfYOdxovc9n4acilVQmy5TgsTcLd0v7AyPEm40Me+ JHJNaV0qtB24mrPDdf4moN69URKoYZD9f0PlU1Jm4wzGIaNZdTNcZNX5tuUn2MEy wtnagDs4pcClGGpFZS3IPgOBW88PlyQrFtgOeUjKKj7MzsqbEfVudfd4amtMqCuk 0xJcSFtmZ533DvFfvnrBC8x6jrYSEIU51LsNUbLW9fTiBWt4ewVzXuITQlkzy2nV eaUu6Wb3X1IWeTtFXrwQVG3BmLxZ+NMp6fFA1Aq9g9J8dQv0sF2Bi+u0srryc5AU JURKoqCADXUCnzzNWTgZDCmQJmm2zaiYxUeXd62rjExtoD+vNbVZx7pIQ7jtMmGr NqyDCgApmxkprD/AVOJ6aNV0ffJ5/I5lpVKSGLTgwMkESz9Yd3nIhe9tjK456pKT 4obmm6iw43jZ0t6u120qgSSLOR018GtwUrUWKO5+kUAsBOhhFMdmhvKMpdQ7Sglv trjla9FmamzInN0b5wcW5xbiJ3yLDuECk02omvZeB5PMPamQLdcR572QMZD49AIO rqgF/xcNgBt6T+i4f32qbpg0FsusPsgG4GH9a85vtJDoONneULcqe7x3yGzHz1pU 9IF2nlo56Mj1ZHXUgZDP =B8T8 -----END PGP SIGNATURE----- --nIQuhgDFDFWb8nGpDe2X6jqH226XwLkKb--