Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80166 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 67976 invoked from network); 5 Jan 2015 01:05:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Jan 2015 01:05:51 -0000 Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.179 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.179 mail-we0-f179.google.com Received: from [74.125.82.179] ([74.125.82.179:60387] helo=mail-we0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1E/50-00230-E63E9A45 for ; Sun, 04 Jan 2015 20:05:51 -0500 Received: by mail-we0-f179.google.com with SMTP id q59so6970889wes.24 for ; Sun, 04 Jan 2015 17:05:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=TO2LUru6KJv8fPO/bnhh61DTih60jRF6v3tviKlaVGc=; b=Nun7lrbCp61pR/Dn3gq5o4V3wYKqn+NOd82ZeJ60l5YaLHNoD8HglXFKsAbSdNVAIG AVbnd9GSlqNupD2k+k4pEMO33HRqA1+8PVcSht4qlTt2pgrvuwxyZA33nRUkfiCpqWW+ kzgOKR3PI6rApLKmZAoAQ3U4RSpBcGm6CeFP2of0iuHdddO7qJ91KBOg8NDAwlg1pP+/ 1wDE2MM7/Wtke5ngwlRbb5VUVvpwnmZ0uLmqGmyJLtFom2Q6tE5/IL51I9meOSWGNJon Ij3I9Av0QEyun0b9FU+qhNHNH9o4OFRIKB4FLsrRM4JJX9diVmZHtowzRFXno512reMQ Tt4w== X-Received: by 10.180.105.68 with SMTP id gk4mr20156831wib.30.1420419947778; Sun, 04 Jan 2015 17:05:47 -0800 (PST) MIME-Version: 1.0 Received: by 10.194.82.163 with HTTP; Sun, 4 Jan 2015 17:05:27 -0800 (PST) In-Reply-To: <002b01d02879$17ce3c10$476ab430$@tekwire.net> References: <002b01d02879$17ce3c10$476ab430$@tekwire.net> Date: Mon, 5 Jan 2015 02:05:27 +0100 Message-ID: To: francois@tekwire.net Cc: PHP Internals List Content-Type: multipart/alternative; boundary=f46d04426a606b5c06050bdd4a61 Subject: Re: [PHP-DEV] Friend classes revived From: ocramius@gmail.com (Marco Pivetta) --f46d04426a606b5c06050bdd4a61 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 5 January 2015 at 00:49, Fran=C3=A7ois Laupretre = wrote: > Hi, > > I know the subject was discussed years ago, and it was decided not to > implement 'friend class' declarations (see > https://bugs.php.net/bug.php?id=3D34044). > > But the discussions occurred ten years ago, and things (and people) may b= e > different now. > > Unfortunately, there is no easy-to-use and fast solution for this yet. Mo= st > projects have asked for such a feature for years but, till now, they stil= l > have to rely on a big warning in the documentation. Some, like symfony, > also > add an '@internal' tag in their phpdoc block. This can be checked in an I= DE > but that's information, not access restriction. > > IMHO, such a feature would be a valuable addition for PHP 7. If you agree= , > I > will write an RFC. > > The feature I was thinking about would be globally similar to C++ friend > classes, except that a PHP friend class would have access to protected > resources, but not private ones (private would remain private). > > The inheritance rules would be similar to C++ rules (adapted from Wikiped= ia > as it originally includes access to private resources) : > > * Friendships are not symmetric =E2=80=93 If class A is a friend of class= B, class > B > is not automatically a friend of class A. > > * Friendships are not transitive =E2=80=93 If class A is a friend of clas= s B, and > class B is a friend of class C, class A is not automatically a friend of > class C. > > * Friendships are not inherited =E2=80=93 A friend of class Base is not > automatically a friend of class Derived and vice versa; equally if Base i= s > a > friend of another class, Derived is not automatically a friend and vice > versa. > > * Access due to friendship is inherited =E2=80=93 A friend of Derived can= access > the > protected members of Derived that were inherited from Base (simpler than > the > C++ rule as friend classes and derived classes have the same access > rights). > > Another idea would be to base access restriction on namespaces, but, IMO, > namespaces would not bring the precision we need : each class needs to > define which classes are accepted as 'friends'. > > Please comment. > > Regards, > > Fran=C3=A7ois > There is no need for friend classes in order to support this kind of feature (see http://ocramius.github.io/blog/accessing-private-php-class-members-without-= reflection/ ) Except for voodoo-cases (AOP frameworks and such) I don't see much of a use-case here. If you need to sync up properties between two classes you can do so by simply using closures, as I described above. Otherwise, making properties `public` and marking them as `@internal DO NOT TOUCH THIS!` is perfectly fine, and doesn't need language-level restrictions IMO. If you need this sort of feature, consider looking at the recently discussed https://github.com/php/php-src/pull/947, which is really much more useful and less magic :-) Greets, Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --f46d04426a606b5c06050bdd4a61--