Req #34044 [Com]: Support for friend classes
| From: | neel dot basu dot z at gmail dot com | Date: | Tue, 04 May 2010 05:40:26 +0000 |
| Subject: | Req #34044 [Com]: Support for friend classes | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-150141@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=34044&edit=1
ID: 34044
Comment by: neel dot basu dot z at gmail dot com
Reported by: arendjr at gmail dot com
Summary: Support for friend classes
Status: Wont fix
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: *
PHP Version: *
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2006-10-11 19:01:09] helly@php.net
Long ago we decided against 'friend' declarations.
------------------------------------------------------------------------
[2006-10-11 12:22:24] 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-09-03 18:34:38] 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-06-16 21:34:45] 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-01-11 00:49:08] 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 )
...
}
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/bug.php?id=34044
--
Edit this bug report at http://bugs.php.net/bug.php?id=34044&edit=1