Re: Extending an instantiated class
| From: | David Harkness | Date: | Sun, 16 Oct 2011 08:45:26 +0000 |
| Subject: | Re: Extending an instantiated class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-315318@lists.php.net to get a copy of this message | ||
On Sat, Oct 15, 2011 at 7:01 AM, Alain Williams <addw@phcomp.co.uk> wrote:
> I have an application where a Screen (web page) may contain several Forms.
> The Forms
> will want to access properties, etc, from their Screen. So what I want is
> to do
> something like:
>
You're using an is-a relationship between Form and Screen, but I think has-a
would be better since a Screen has one or more Forms. Keep the $screen
property in Form, but break the inheritance relationship. Define methods in
Form to allow you to set properties on its containing Screen.
class Form
{
private $screen;
public function __construct(Screen $screen) {
$this->screen = $screen;
}
public function AddJsVar($name, $value) {
$this->screen->AddJsVar($name, $value);
}
public function GetJsVar($name) {
return $this->screen->GetJsVar($name);
}
}
Now adding a var to a Form passes it on to its owning Screen. It's also a
lot simpler than using reflection.
Peace,
David