CodeIgniter Forums
Subclassing vs hooks - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Subclassing vs hooks (/showthread.php?tid=54399)



Subclassing vs hooks - El Forum - 09-07-2012

[eluser]jefc[/eluser]
Hi,

I'll try to express this as best as I can - if one has a class which is such that once an instance of it is initialised, information therein may indicate that some special processing is required, outside of the norm, and from a selection of several alternatives.

My insticts tell me (I am fairly new to OOP though...) that this is a prime candidate for subclassing: I should create a subclass of the above which over-rides certain methods with ones which add the extra processing.

The sticking point I am at though is that the app does not know that it needs to instantiate the subclass instead of the superclass, so until I find a better solution I am creating an instance of the superclass, checking if a subclass should be used instead and if so, destroying the instance of the superclass and creating an instance of the subclass, based on the same inputs as the object I have just destroyed. I am fairly certain this points to problems with my overall architecture. Although I did wonder if I can use hooks. So when the object is instantiated it can register certain hooks which are checked for at certain places in processing. Still, I thought OOP existed for exactly this kind of situation...

Apologies for the long question - hope it makes sense.

Btw - this is a reporting dashboard app. There are 'vanilla' reports, but there are also more exotic variants which require extra number-crunching steps.

Cheers,
Geoff