![]() |
|
XP is just a number | |
PerlMonks |
Overloading inherited class methodsby Ovid (Cardinal) |
on Dec 04, 2002 at 17:54 UTC ( #217540=perlquestion: print w/replies, xml ) | Need Help?? |
Ovid has asked for the wisdom of the Perl Monks concerning the following question: I seem to have coded myself into a design corner and I'm trying to figure the best way out. I have a base class that I'll call Foo::PersistentObject. This class is not to be instantiated, but instead, objects will inherit some common methods from it:
The intent is to make all objects that use this class persistent in the database without the application programmer needing to worry about it. The applications in the poop group didn't seem applicable for various reasons (need gradual refactoring, database limitations, etc.). The problem is that the get_list method returns a list of objects, but the constructor has been overloaded and it's causing some problems as a result. Here's how a subclass might use it:
Note: The intent of the %mapping hash is to provide external method names that map to internal field names so that I can change field names in the database without the programmer needing to worry about the interface. The programmer could use this class as follows:
The problem lies in the fact that I chose to overload the constructor. See that line with the _open_this_id line? Well, if I call get_list, the super class method looks like this:
You can see that I am creating a list of objects and as I am now passing an argument list to new, it thinks that there is an id to open, but this id winds up being one of the keys of the %args hash. This causes my id to be incorrect and the constructors fail. I got around this by doing the following:
Then, in the Foo::PersistentObject::get_list method, I call the _new class method instead of new. It seems to solve the problem, yet it also seems a bit hackish at the same time. Having to simulate overloading based on method signatures is making this code considerably more ugly. Can anyone offer advice on a better strategy here? Also, I'm curious to know if the whole idea might be unsound. I think that it's reasonable, but I know there are those who assert that inheritance should be avoided in Perl. Cheers, New address of my CGI Course.
Back to
Seekers of Perl Wisdom
|
|