How to specify "friend" visibility in php
How to specify "friend" visibility in php
am 29.01.2008 02:45:46 von Daniel Klein
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
Re: How to specify "friend" visibility in php
am 29.01.2008 03:57:07 von zeldorblat
On Jan 28, 8:45 pm, Daniel Klein 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.
Re: How to specify "friend" visibility in php
am 29.01.2008 04:05:02 von Jerry Stuckle
Daniel Klein 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
>
Daniel,
No formal standard like you're thinking; rather a set of informal
standards. The problem is, everyone has their own :-)
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: How to specify "friend" visibility in php
am 29.01.2008 15:23:13 von Daniel Klein
On Mon, 28 Jan 2008 18:57:07 -0800 (PST), ZeldorBlat
wrote:
>On Jan 28, 8:45 pm, Daniel Klein 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
Re: How to specify "friend" visibility in php
am 29.01.2008 20:40:51 von Jerry Stuckle
Daniel Klein wrote:
> On Mon, 28 Jan 2008 18:57:07 -0800 (PST), ZeldorBlat
> wrote:
>
>> On Jan 28, 8:45 pm, Daniel Klein 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
==================
Re: How to specify "friend" visibility in php
am 29.01.2008 20:56:38 von Steve
"Jerry Stuckle" wrote in message
news:y66dnRXY6-c9HALanZ2dnUVZ_vDinZ2d@comcast.com...
> Daniel Klein wrote:
>> On Mon, 28 Jan 2008 18:57:07 -0800 (PST), ZeldorBlat
>>
>> wrote:
>>
>>> On Jan 28, 8:45 pm, Daniel Klein 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.
in addition to the note, it is sometimes a good idea to prefix these with an
underscore. it's a good reminder and make a caller less likely to access it
unless he knows it's there are how it's used. it's good too if you want
public accessors to that functionality on a limited/semi-restricted
basis...the names won't conflict and will visually relate as you read
through the code. either way, it helps denote that it is not meant for
general consumption and is commonly understood in php.