Re: Decorator with public methods

From: Date: Sat, 27 Dec 2008 19:56:26 +0000
Subject: Re: Decorator with public methods
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-285109@lists.php.net to get a copy of this message
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) } } Ken

« previous php.general (#285109) next »