Friend classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Colin Fine

    Friend classes

    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)

    Colin
  • Oli Filth

    #2
    Re: Friend classes

    Colin Fine said the following on 19/10/2005 00:19:[color=blue]
    > 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:
    >[/color]

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


    --
    Oli

    Comment

    • Mike Willbanks

      #3
      Re: Friend classes

      > PHP OOP is *extremely* limited. I'm afraid you're going to have to live[color=blue]
      > with it... ;)[/color]

      Hey who are you calling limited >;)

      --
      Mike Willbanks
      Zend Certified Engineer

      Comment

      Working...