[PLUG] PHP class instances

Wil Cooley wcooley at nakedape.cc
Wed Jun 25 07:51:02 UTC 2003


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
-- 
Wil Cooley                                 wcooley at nakedape.cc
Naked Ape Consulting                        http://nakedape.cc
* * * * Linux, UNIX, Networking and Security Solutions * * * *
*     Tired of spam and viruses in your e-mail?  Get the     *
* Naked Ape Mail Defender! http://nakedape.cc/r/maildefender *
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.pdxlinux.org/pipermail/plug/attachments/20030625/b00df5b6/attachment.asc>


More information about the PLUG mailing list