Patches

Pull Requests

History

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 )
...
}

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.

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.