FAQ
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 )
...
}

There is feature request on bugs.php.net (#34044) about this already, but no
comments. Is there any support for this sort of thing out there?

Useful addition to PHP6 maybe, or just another
unnecessary feature? :-)

Cheers,

Leon

Search Discussions

  • Greg Beaver at Jan 11, 2006 at 4:38 am

    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
  • Hartmut Holzgraefe at Jan 11, 2006 at 9:06 am

    Greg Beaver wrote:
    By definition, if you're accessing it from another class, then perhaps
    it isn't "private" at all.
    in C++ "friend" gives access to both "private" and "protected" ...
    This can be more easily and intuitively
    solved by using protected and CoderTest extends Coder,
    given that PHP doesn't have multiple inheritance this just doesn't
    work out all the time, so this would actually be an argument *for*
    "friend" in PHP
    or just making it
    public and documenting in the code where it should and should not be used.
    this argument can easily be extended to "don't use the PPP stuff at all"

    "friend" on the other hand is a mechanism to allow more fine grained
    access control

    "friend" extends PPP in a similar way as ACLs extend the unix file
    access control mechanisms "user/group/others" concept ...

    --
    Hartmut Holzgraefe, Senior Support Engineer .
    MySQL AB, www.mysql.com
  • Derick Rethans at Jan 11, 2006 at 7:30 am

    On Wed, 11 Jan 2006, Leon Matthews wrote:

    There is feature request on bugs.php.net (#34044) about this already, but no
    comments. Is there any support for this sort of thing out there?
    If you only need friends for testing the contents of static properties,
    then there are alternative ways. One solution might be to enhance
    reflection to be able to peek into those objects (or did we already
    allow this?). A second solution is:
    http://derickrethans.nl/private_properties_exposed.php

    regards,
    Derick
  • Sebastian Bergmann at Jan 11, 2006 at 10:05 am

    Derick Rethans schrieb:
    A second solution is:
    http://derickrethans.nl/private_properties_exposed.php
    The mechanism above is emplyed by PHPUnit 3.0 to allow testing private
    and protected properties.

    --
    Sebastian Bergmann http://www.sebastian-bergmann.de/
    GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
  • Ilia Alshanetsky at Jan 11, 2006 at 2:21 pm
    I think we should avoid complicating an already complex OO
    functionality. Borrowing concepts from other languages is fine, but to a
    degree.

    Ilia
  • Lukas Smith at Jan 11, 2006 at 2:32 pm

    Ilia Alshanetsky wrote:
    I think we should avoid complicating an already complex OO
    functionality. Borrowing concepts from other languages is fine, but to a
    degree.
    I think the PHP real world (not the guys porting Java APIs to PHP)
    requires support binary only:
    "public API"
    "my break at any time API"

    regards,
    Lukas
  • Andi Gutmans at Jan 11, 2006 at 2:50 pm
    Yep, I agree with that. And coming from years of writing C++ I barely
    used friends and it always seemed kind of hackish to me.
    At 06:21 AM 1/11/2006, Ilia Alshanetsky wrote:
    I think we should avoid complicating an already complex OO
    functionality. Borrowing concepts from other languages is fine, but to a
    degree.

    Ilia

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedJan 10, '06 at 11:15p
activeJan 11, '06 at 2:50p
posts8
users8
websitephp.net

People

Translate

site design / logo © 2022 Grokbase