Re: Support for friend classes

From: Date: Wed, 11 Jan 2006 04:38:12 +0000
Subject: Re: Support for friend classes
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-21470@lists.php.net to get a copy of this message
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

« previous php.internals (#21470) next »