|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34044 Support for friend classes
Submitted: 2005-08-09 02:11 UTC Modified: 2006-10-11 19:01 UTC
Avg. Score:4.6 ± 0.8
Reproduced:99 of 103 (96.1%)
Same Version:65 (65.7%)
Same OS:63 (63.6%)
From: arendjr at gmail dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: * OS: *
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-08-09 02:11 UTC] arendjr at gmail dot com
It would be really nice if PHP5 would support "friend classes", like C++ does. This way, classes can give access to their private members to other classes.

Reproduce code:
class Foo
  friend class Bar; // using C++ syntax

  private static $a = 5;

class Bar
  public static function printA()
    echo Foo::$a;


Expected result:
the above example would then print: "5"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-11 00:49 UTC] php at lost dot co dot nz
This would also be really useful for me writing unit tests so that I can run 'white box' testing on internal private methods another class (like I used to do back in the 'good' old days of PHP4... ;-)

Something like this:

class Util
  friend class UtilTest;
  private static function foo() {...}

class UtilTest
  if( Util::foo() != $expected ) 
 [2006-06-16 21:34 UTC] tsteiner at nerdclub dot net
In PHP 5.1, it is possible to simulate the function of friend classes with __get and __set (I think PHP 5.0 triggers an error when trying to access private properties even with __get or __set):

class HasFriends
    private $__friends = array('MyFriend', 'OtherFriend');

    public function __get($key)
        $trace = debug_backtrace();
        if(isset($trace[1]['class']) && in_array($trace[1]['class'], $this->__friends)) {
            return $this->$key;

        // normal __get() code here

        trigger_error('Cannot access private property ' . __CLASS__ . '::$' . $key, E_USER_ERROR);

    public function __set($key, $value)
        $trace = debug_backtrace();
        if(isset($trace[1]['class']) && in_array($trace[1]['class'], $this->__friends)) {
            return $this->$key = $value;

        // normal __set() code here

        trigger_error('Cannot access private property ' . __CLASS__ . '::$' . $key, E_USER_ERROR);
 [2006-09-03 18:34 UTC] linus dot martensson at elplan-gm dot se
I'm developing an OOP CMS, and the lack of friend objects prevents me from disallowing layouts access to unsafe internal variables. It's actually a rather severe problem when you try writing an advanced system.
 [2006-10-11 12:22 UTC] goliath dot mailinglist at gmx dot de
I'm developing an OOP CMS, too. This feature is really elementary for complex systems where you want to limit access to the internals. Currently I developed a system to access protected methods from outside (using one base-class, a list  to control the access and some secure handshake when accessing the method), but thats detouring. Would be nice to see some friend-system (like C++, or package -systemlike Java) in PHP6.
If someone is interested in the code I mentioned above, I can post it.
 [2006-10-11 19:01 UTC]
Long ago we decided against 'friend' declarations.
 [2010-05-04 07:40 UTC] neel dot basu dot z at gmail dot com
Ya friend functions would be a nice thing.
as PHP doesn't support many interesting keys of OOPS like Template, friend etc.. 
(I know Java doesn't support most of them too, But thats not a good excuse, cause 
in PHP some of this can be supported with minimum change in Code) Its not 
possible to maintain Proper Coding Standard. and Programmers are forced code in a 
bad way. designing patterns/idioms cannot be followed.
 [2010-06-06 19:22 UTC] alsarg72 at gmail dot com
Yeah. PHP's fundamental OO credentials are very poor in general
for many reasons that unfortunately are in ways that are not
fixable without completely replacing the language because of
really basic stuff like so much being in the global namespace,
it's reliance so heavily on global variables, etc, and
developers who work with PHP and have not come from a language
that does a decent job of OO can't really be expected to know
much different. I'm hoping to get back to C++ soon.
 [2013-05-29 15:56 UTC] daniel dot bingham at ellislab dot com
Consider the example of an in place parser for an XML like language.  You might split that into 3 classes:

File - the file being parsed, stores the actual string
Parser - the object that does the parsing, only needed at time of parsing
Tag - a tag in the file, stores the location of the tag within the string

You only want to store the text of the file in a single place and store the location of tags with in the file.  So Tag needs access to File's internals (for the string).  The Parser needs access to both object's internals, in order to set the locations during parsing.  But the user of these classes should have access to neither of these things except through the public interface. The designer chooses which methods to expose to ensure the file is being manipulated in a legal and reasonable way.

As it stands right now, this is impossible.  Anything that Parser needs to access in either File or Tag must be public.  Meaning that users of the module can access it and potentially manipulate the file in an illegal way.  This is not good interface design.  As a result of this, in attempting to build any sort of complex, modularized system in PHP, one ends up with tons of comments to the effect of "Yes I know this is public, but DON'T CALL IT!" beating the point of access control.

I would strongly urge the PHP dev crew to reconsider adding a friend declaration in a future version of PHP.  Allow friend classes to access protected, but not private, properties and methods - similar to inheriting classes.  This will allow for the development of complex, multi-class modules that expose reasonable and well designed public interfaces.
 [2016-12-21 17:58 UTC] maciej dot helminiak at vp dot pl
I'm missing this feature too. I hope C++ (with WebCPP framework) will make some fuss in web development world soon, for real OOP.
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Mon Jan 23 02:01:41 2017 UTC