Re: Extending an instantiated class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux