|
Posted by Jerry Stuckle on 01/29/08 19:40
Daniel Klein wrote:
> On Mon, 28 Jan 2008 18:57:07 -0800 (PST), ZeldorBlat <zeldorblat@gmail.com>
> wrote:
>
>> On Jan 28, 8:45 pm, Daniel Klein <dani...@featherbrain.net> wrote:
>>> I've got a class that has a couple of methods that can't be 'private' cos
>>> they can be called by other classes in the project, but they should not be
>>> called by user code, so I've had to make them 'public'. In other languages
>>> these methods would have 'friend' visibility.
>>>
>>> I suspect there is no 'friend' access modifier since php has no concept of a
>>> 'module' or 'assembly' (at least none that I am aware of).
>>>
>>> I'm use to coding in dynamic languages where you use the 'honor system'. For
>>> example, in python you indicate this by placing an underscore character in
>>> front of the method/function. You also document the method to indicate your
>>> intentions. So unless there is a similar php convention, I am more than ok
>>> doing it that way.
>>>
>>> So my question is: What is the php way of doing this?
>>>
>>> Daniel Klein
>> None that I'm aware of. If you don't want your user code to call a
>> method, then don't call that method :)
>>
>> Besides, I never liked the whole "friend" concept. Objects should be
>> reusable -- which means that it shouldn't matter who is using them.
>> If you want to use them from "user code" then go for it. If you want
>> to use them from other objects then that's fine, too. You give your
>> methods public visibility and everyone can get to them. If you want
>> to hide something, make it protected or private.
>>
>> Why should certain objects be given special access to the internals of
>> another class while other not-so-special objects don't get any? If
>> you've designed your classes correctly you shouldn't need friends at
>> all.
>
> Although Jerry Stuckle answered my question, I feel compelled to respond
> here...
>
> I think you misunderstood the issue.
>
> The situation is such that there are several classes that work together (in
> .NET terms this would be an 'assembly'). Two of the methods of one of the
> classes perform very special (friendly ;-) ) things for the rest of the
> classes, so it is ok for those 'other' classes to access those methods as
> this is by design. However, it makes no sense whatsoever for code outside of
> these classes to use those methods, hence the original request for some sort
> of PHP convention to indicate this.
>
> Here is a pseudo-code example which is close to what I'm doing (I've placed
> 'friend' in front of the appropriate methods for clarity):
>
> class connection {
> public function open_connection {}
> friend function send_data {}
> friend function recv_data {}
> public function close_connection {}
> }
>
> class resource {
> public function open {}
> public function read {}
> public function write {}
> public function close {}
> }
>
> All of the 'resource' functions use the 'connection->send_data' and
> 'connection->recv_data' methods. Any other code using either the 'send' or
> 'recv' methods have no clue (should not be concerned) what the api is. That
> code will simply use the 'open' 'read' 'write' 'close' methods as that is
> all 'outside code' needs to know about (ie the 'public api').
>
> The 'send' and 'recv' functions can't be 'private' cos they need to be
> accessed from the 'connection' class, and they shouldn't be 'public' cos
> outside code should not them directly, but only via the other 'public'
> methods.
>
> I am open to suggestions on how to restructure this without having to
> duplicate code.
>
> Daniel Klein
>
Daniel,
Unfortunately, there is no real good way to do it in PHP, yet anyway.
Perhaps they will eventually allow friend functions.
For now I just make them public with a note not to call them. Not a
good solution, I know. But better than duplicating code and having to
change something multiple places should conditions change.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|