[nycphp-talk] PHP 5 Interfaces
Michael Hernandez
sequethin at gmail.com
Tue May 26 16:53:42 EDT 2009
On Tue, 2009-05-26 at 16:27 -0400, Joseph Crawford wrote:
> THanks for pointing that out, I guess I am a bit confused as to why
> they are restricted to public but it answers my question :)
>
> Thanks Again.
I think the idea is that by defining and interface you are planning to
implement common methods across multiple classes. If you were going to
make the methods private, you'd probably design your classes in such a
way that the abstract base class contained a protected method that you
could redefine in child classes? Or maybe a private method in a
non-abstract parent class (since a private method in an abstract class
would make no sense, considering you cannot instantiate an abstract
class there'd be no way to call that method).
If you're using an interface at all, it seems that you are doing so
because you want to take advantage of the ability to share common
methods among several classes, or take advantage of PHP's only form of
multiple inheritance, since you cannot extend a class from multiple
parents but you can implement multiple interfaces. If something is
intended to be private, putting it in an interface (which is sort of
designed to be shared) wouldn't really make sense.
I tend to think of an interface in a physical sense, for example the
interface to a door. Every door has some means to open it and close it,
and thus implements a common interface. It's something all doors share.
We couldn't really define how to open every door because some have a
door-knob, some might have a handle, some might use a motion sensor,
some you just push. Same goes for close() - with some doors you push the
door the other way, or turn the handle the other way, or you might even
just let the door go and it will close by itself. Either way, this is
something that all the doors would share(the ability to be opened and
closed), and the knobs or handles or what-have-you, are all intended to
be used by another class (a person?). Having a private method in the
interface that would be shared among all doors wouldn't make much sense.
On the other hand, a magnetic door might have a private
unlockMagnetSensorThing() method, a door with a knob might have private
methods that deal with the knob's internals?
That's how I think of it anyway, I'm not an expert but I hope it might
help!
--Mike H
More information about the talk
mailing list