php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #37814 Php shoul have class friends
Submitted: 2006-06-15 10:33 UTC Modified: 2007-08-07 12:05 UTC
Votes:9
Avg. Score:4.1 ± 1.3
Reproduced:7 of 8 (87.5%)
Same Version:3 (42.9%)
Same OS:1 (14.3%)
From: henke dot andersson at comhem dot se 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:

 [2006-06-15 10:33 UTC] henke dot andersson at comhem dot se
Description:
------------
Php should have a friend structure for classes, like c++.
That way some normaly private things can be used by selected other classes and functions.

An alternative would be to do it like Java with inner classes, but personaly I think that while inner classes could be usefull in php, friend classes should also exist like in c++.

Reproduce code:
---------------
<?php
//my suggestion for the syntax
class A {
 static function callB(B $b) {
  $b->privatefunction();
 }
}

class B {
 friend class A;
 friend function C;

 private function privatefunction() {
  echo 'privatefunction!';
 }
}

function C(B $b) {
 $b->privatefunction();
}

$b=new B();
A::callB($b);
C($b);

Expected result:
----------------
Class A and function C should be able to call B::privatefunction.

Actual result:
--------------
Since this functionality doesn't exist the code wont even compile.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-06 22:29 UTC] michael at chunkycow dot com dot au
The whole C++ friends thing is a mess, use an interface between your super friendly classes and simply don`t use it if the classes aren`t joined at the hip, this would even help to keep nice low coupling and aid re-use.
Private inner classes have some merit but are not a cure for common sense.
 [2007-08-07 12:05 UTC] helly@php.net
We decided against those complex features.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 25 07:02:14 2014 UTC