Edit report at https://bugs.php.net/bug.php?id=34044&edit=1
ID: 34044
Comment by: daniel dot bingham at ellislab 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: *
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2010-06-06 19:22:18] 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.
------------------------------------------------------------------------
[2010-05-04 07:40:25] 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.
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
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
https://bugs.php.net/bug.php?id=34044--
Edit this bug report at https://bugs.php.net/bug.php?id=34044&edit=1