|
Problem:
You want your class to inherit from an instance of another class. Solution:
Just do it:
Discussion: This is an extension of the technique we used in the last recipe, where we subclassed an instance of a builtin type. We can in fact specify any object instance as a parent of a class, with similar effects. This approach is used extensively when creating classless object frameworks. This technique should be used sparingly, and only when you really mean it. Because the data in the instance can be modified at any time, there's no way for perl to cache even the most basic information about what the inheritance graph looks like, what attributes and what methods are provided by what objects, etc. Issue: Making changes to a parent instance while in a method of the derived class could have bizzare results. But if you did that, you probably meant to do that. Issue: This approach is somewhat different from that presented in recipe 3.13, "Creating 'Classless' Object Hierarchies". They need to be reconciled.
|