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: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
4 + 7 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 10:01:28 2024 UTC