Re: A Good OOP Tutorial/Read?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, May 17, 2013 at 4:04 PM, Tedd Sperling <tedd.sperling@xxxxxxxxx>wrote:

> Stuart:
>
> You said:
>
> > An interface does what it says on the tin: it describes an interface
> that a class can then tell the world it implements.
> >
> > An abstract class provides functionality as well as an interface
> description. An abstract class cannot be instantiated, it can only be
> extended.
> >
> > The logging example given by someone earlier in this thread is the most
> common example given for interfaces. This is where the interface is defined
> as an API for others to implement and/or use which then enables users to
> pick and choose the combination of implementations they want to use based
> on their requirements without needing to change any of the code of either
> class.
>
> I understand the "stated" differences between abstract and interface. I
> can cite what the differences are, but I don't see the practical
> application differences. To me there is no difference between an abstract
> class (without method declarations) and an interface.
>
> However, I view an interface as a statement (a contract) where IF you want
> someone to use your code you outline the methods you require them to
> flesh-out in their code  -- but I would like to see a simple example of
> that.
>
> I vaguely get the logging example given by Larry, but I'm not good at
> abstract thinking -- I need a concrete simple example.
>
> I tried to create a demo where I had a Toaster Class that contained
> breadNumber() and toastSetting() methods and then created an interface so
> my students could use the Toaster, but it didn't really hold up as well as
> I wanted.
>
> So, can anyone give me a simple example where an interface is used so I
> can easily explain why they are important?
>
> Thanks,
>
> tedd
>

There are plenty, but they are never required. I see interfaces more as
some kind of documentation in PHP code, they have much more advantages in
other languages (eg. Java).
A good example would be if you build a product that allows plugins to be
used. You would create an interface (which requires the functions init,
execute and finish for example), and state that any plugin should implement
that interface and hese functions.
It's the cleaner way of doing things, but it surely would work the same as
if you didn't use an interface, and just told in your documentation that a
class should have these 3 functions.

- Matijn

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux