On Sat, Oct 15, 2011 at 7:01 AM, Alain Williams <a...@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

Reply via email to