[PLUG] PHP class instances

Shahms King shahms at shahms.com
Wed Jun 25 08:01:01 UTC 2003


You can also use it to emulate a number of features of C++ templates
(albeit, at runtime and with a different syntax . . .)

On Wed, 2003-06-25 at 07:50, Wil Cooley wrote:
> On Wed, 2003-06-25 at 01:25, Daniel B. Herrington wrote:
> > PHP gurus,
> > 
> > I discovered that I can instantiate classes with a variable name. i.e.:
> > 
> > I create a class called test, then in my script:
> > 
> > $var = test;
> > $testclass = new $var;
> > 
> > and a new instance of test gets created. I was wondering if this was bad
> > practice and if I should cease and desist before I make a habit of creating
> > instances of classes this way?
> 
> This is a feature that can be used with the AbstractFactory design
> pattern, to instatitate objects which adhere to an interface but whose
> exact implementation should be decided at run-time.
> 
> There are other places you can use this that I'm not thinking of right
> now.  Obviously in your test example, there's no need for it, but it can
> very useful.  So, use it when it actually solves a problem and don't
> when it doesn't.
> 
> Wil
-- 
Shahms King <shahms at shahms.com>





More information about the PLUG mailing list