Forum Moderators: coopster

Message Too Old, No Replies

Interface or Inheritance?

Fairly advanced PHP4 OOP

         

Warboss Alex

4:22 am on Jan 30, 2005 (gmt 0)

10+ Year Member



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 ...

jollymcfats

5:10 am on Jan 30, 2005 (gmt 0)

10+ Year Member



If you're inheriting you still need to remember to make the methods, as there are no abstract classes in php. You could make the base-class methods throw an exception as a reminder.

Personally I would go the PHP 4 "interface" route, aka duck typing. Then your registry clients are free to inherit from whatever makes sense for them. You might use method_exists() in your registry code as a guard against an incomplete implementation.