SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Have I correctly understood Object Interfaces?

    Hi,
    moving from PHP4 to PHP5 and getting familiar with the new Object Model.
    I was wondering a little something about Object Interfaces.

    When you implement an Interface, you must implement all the methods in the Interface as well, if not a fatal error will be issued.

    What seems strange to me is that you don't define at all what the methods do. Then what's the use of implementing them? Is that just to force a set of classes to share the same methods?

    I can't get the idea behind Interfaces at the moment.... I'm sure it will seem bright as a blue sky in a few days but today I don't get it.

    Does anyone feel like enlighting me?


  2. #2
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure. Well interfaces are similar to abstract classes: you can implement them and thus override their methods. Interfaces however may not be partially implemented already like abstract classes - you have to implement all of the interfaces methods. In return, you may implement more than one interface at the same time while you can only extend one base class in class inheritance.

    Therefore you can see the Interface-mechanisms as a compromise for the missing multiple inheritance in PHP. Class (!) inheritance defines is_a relationships (class Rectangle extends the Base class Shape -> A Rectangle is a Shape) while object composition defines has_a relationships (A car has a wheel - even four of them). PHP, like Java, does not allow multiple is_a relationships. Therefore they define interfaces to make up for that.

    So what are interfaces and what do they do?

    1. All methods in interfaces are implicitely abstract.

    2. An interface defines a public API, thus all methods are implicetely public, although this modificator is not mentioned. You get an error if you define methods in an interface as private or protected.

    3. The only attributes that are allowed in interfaces are static or final attributes. Instance attributes are part of the implementation and thus do not belong to interfaces. So, duuudie, here you see again that an interface is only an api specification.

    4. An interface can't be instantiated and thus has no constructor.

    So when should you use interfaces in favor of abstract classes? That's difficult, since both have similar properties. It will be difficult to implement interfaces that have a lot of methods, since they define no implementation and thus repeating the same implementation for every class that implements the same interface can be violating the DRY-principle (don't repeat yourself).

    Once you add a method to an existing interface, all classes that implement this interface will not work anymore, since they have to implement all operations of the interface.

    Too bad I can't give you a rule-of-thumb when to use what. It's most of the time good to use both. You should just make sure that interfaces dont get too complex (i.e. have many methods) or else they won't be reusable. Also, interfaces are often called with adjectives that describe an ability, such as "Nameable", "Destroyable", etc, which should give you a good idea on what to use them for.

    Hope this gave you a good start.

  3. #3
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by duuudie
    Is that just to force a set of classes to share the same methods?
    Exactly. It is called "typing" -- when you implement an interface you effectively give a type to an object, and all objects of the same type share the same behavior (i.e. classes). Imagine a Vehicle interface, which declares the method move() -- in implementation, a Car or a Plane classes will have wildly different implementations, but as they both implement the interface Vehicle we'll know that they both must be able to move().

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    If your at the stage where you want to learn more about Interfaces, maybe this link will help to further your understanding?

    http://www.javaworld.com/javaworld/j...7-java101.html

    Java World is a wonderful resource of information not only on Java but Object Oriented Programming in general (which I why I'm never away from the site )

  5. #5
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much.

    the comments and the link really helped a lot


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •