Friend classes

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Now that PHP5 has proper scoping for class properties and methods, I've  
been looking in vain for friend classes, or wondering if there's another  
way to achieve this effect:

class A {

    protected var $v1;



class AHelper {


   function ....

       $helper = new AHelper ( ... );

       if ($helper->v1) ...


So A has a property which really is not supposed to be public, but  
AHelper (which does not inherit from A) has a special relationship with  
A and wants to be able to delve inside it.

Note that just providing a public accessor won't help: it's logically  
the same as making the property public.

C++ manages this by having 'friend classes' - classes which are allowed  
inside a class even though they are not derived from it.

I suppose I could write a public accessor method which takes its caller  
as an argument and checks its type before it will go ahead and fetch the  
value; but that's inelegant, heavy, and only takes effect at run time.

Has anybody got any better suggestions?

[I see somebody asked the same question two weeks ago on faqts, but did  
not get an answer)


Re: Friend classes

Colin Fine said the following on 19/10/2005 00:19:
Quoted text here. Click to load it

PHP OOP is *extremely* limited. I'm afraid you're going to have to live  
with it... ;)


Re: Friend classes

Quoted text here. Click to load it

Hey who are you calling limited >;)

Mike Willbanks
Zend Certified Engineer

Site Timeline