Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:21470 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25480 invoked by uid 1010); 11 Jan 2006 04:38:24 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 25465 invoked from network); 11 Jan 2006 04:38:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Jan 2006 04:38:24 -0000 X-Host-Fingerprint: 69.64.38.41 bluga.net Linux 2.5 (sometimes 2.4) (4) Received: from ([69.64.38.41:50639] helo=bluga.net) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id B7/13-65277-0CB84C34 for ; Tue, 10 Jan 2006 23:38:24 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by bluga.net (Postfix) with ESMTP id D3456538AE0; Tue, 10 Jan 2006 22:40:39 -0600 (CST) Received: from bluga.net ([127.0.0.1]) by localhost (bluga.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 02251-16; Tue, 10 Jan 2006 22:40:39 -0600 (CST) Received: from [192.168.0.101] (CPE-24-208-79-238.neb.res.rr.com [24.208.79.238]) by bluga.net (Postfix) with ESMTP id 52CFA538ADF; Tue, 10 Jan 2006 22:40:39 -0600 (CST) Message-ID: <43C48BB4.2040303@php.net> Date: Tue, 10 Jan 2006 22:38:12 -0600 User-Agent: Mozilla Thunderbird 1.0.7 (X11/20051128) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Leon Matthews CC: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new using ClamAV at bluga.net Subject: Re: [PHP-DEV] Support for friend classes From: cellog@php.net (Greg Beaver) Leon Matthews wrote: > Hi. > > I'm responsible for writing unit tests for our PHP projects at work. > > I would find C++ style friend classes really useful for getting at another > class's private methods. Since we adopted PHP5 we have been using lots of > private methods to clean our interfaces up. Great news for everyone except > me! > > I'd like to be able to do something like: > > class Coder > { > friend class CoderTest; > > private static function encode() > { > ... > } > } > > class CoderTest > { > $expected = ... > $actual = Coder::encode(...); > if( $expected != $actual ) > ... > } By definition, if you're accessing it from another class, then perhaps it isn't "private" at all. This can be more easily and intuitively solved by using protected and CoderTest extends Coder, or just making it public and documenting in the code where it should and should not be used. This is one reason that "private" should be used sparingly, and perhaps should be thought of more as "final" than as anything else - once you declare something private, it is inaccessible. Greg