On Sat, Dec 27, 2008 at 12:56 PM, phphelp -- kbk <[email protected]>wrote:
> On Dec 26, 2008, at 7:53 PM, Larry Garfield wrote:
>
> I have an object to which I want to add behavior (methods). I cannot use
>> inheritance here because the object is already of a type or subtype (vis,
>> I am
>> already using inheritance for something else), and because I want to be
>> able
>> to add multiple types of behavior at runtime.
>>
>
> If I understand what you are wanting to accomplish, perhaps a simpler
> solution:
>
> Whenever I create a function in a core-level class, I add pre- and post-
> execution language to it. For example:
>
> class foo_bar {
>
> function foo () {
> if (!on_foo()) {
> return FALSE;
> }
> // normal foo code
> // normal foo code
> // normal foo code
> // normal foo code
> // normal foo code
> if (!more_foo()) {
> return FALSE;
> }
> }
>
> function on_foo() {
> return TRUE;
> }
>
> function more_foo() {
> return TRUE;
> }
> }
>
> -- then in the child class:
> class child_bar extends foo_bar {
>
> function on_foo() {
> // runtime code
> // runtime code
> // runtime code
> // runtime code
> return TRUE; // (or FALSE)
> }
> }
more commonly referred to as the template method.
-nathan