Re: [PHP] Extending an instantiated class

2011-10-16 Thread David Harkness
On Sat, Oct 15, 2011 at 7:01 AM, Alain Williams  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


[PHP] Extending an instantiated class

2011-10-15 Thread Alain Williams
Well, that is what I think that I need. Please let me explain what I am trying 
to do
and tell me how to do it or a better way of doing it.

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:

class Screen {
private $JavascriptVars = array();

function AddJsVar($name, $val) {
  $this->JavascriptVars[$name] = $val;
}
}

class Form extends Screen {
public $screen;  // Could set to a class Screen

function DefineForm($fName)
.
}

$s = new Screen();
$s->AddJsVar('Date', '15 Oct 2011');

$f1 = new Form($s);
$f1->DefineForm('search');
$f1->AddJsVar('CompanyName', 'IBM');

$f2 = new Form($s);
$f2->DefineForm('login');
$f2->AddJsVar('Error', 'Login failed');

The trouble is that $f1->AddJsVar() will fail, class extention seems not 
designed to work like that.

I could make it work by either:

1) $f1->screen->AddJsVar() --- but I would rather avoid the extra '->screen'.

2) Use of a __call() in class Form -- but that makes things slower.


Any help gratefully received.



I think that to do what I want PHP would need a syntax like:

$f2 = new $s Form();
or
$f2 = $s->new Form();

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT 
Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: 
http://www.phcomp.co.uk/contact.php
#include 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php