SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2004
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Interface or Inheritance?

    Hello everyone,

    I want to build a Registry object for my application, holding resources for the rest of the script such as a database connection, a custom session instance, etc etc. The Registry holds an array of references to these objects, and initialises them.

    If each of those 'registerable' objects needs to have a setUp and tearDown method for the Registry to call (setUp would start the session, connect to the database etc, while tearDown would end the session, close the connect etc..) what's the best way to implement this?

    An interface 'Registerable' seems to make more sense to me, even though I'm using PHP4 and will have to remember to make all the classes include those methods, although the same can be achieved by having all the classes extend a Registerable class, so over-riding the setUp and tearDown methods.

    What's best?

    Cheers,
    Alex ...

  2. #2
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quite often I will create an interface for what I need and then create a abstract base class. I often tend to do this with classes where I have a number of decorators. The base decorator class is abstract, and implements the interface. Concreate classes and concrete classes extending the decorator base will also implement this interface.

    That way I can use the interface name for strict typing in function/method parameters. This way I avoid "Concrete Class Dependency" which is a form of tight coupling.

    In PHP4 you don't have interfaces, so a base class that you extend from seems reasonable. If want to, you can do something like WACT's is* methods to loosly implement some form of interface system and use method_exists() to check for this special method, in order to replace instanceOf.

  3. #3
    SitePoint Zealot
    Join Date
    May 2004
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree that base class and extending is probably the better option, coding-wise, but I think about the object hierarchy and think:

    DataSource->Database->MySQL (implements Registerable)

    Is so much more sensible than:

    Registerable*->DataSource->Database->MySQL

    *I suppose I'd call it 'RegistryItem' or something..

    But I AM using PHP4 so it's probably sounder coding to use the base class. Specifying the base method bodies as being just a call to die('this is abstract') means I won't forget anything either..

  4. #4
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    Interface .

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things


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
  •