You are here: Re: factory pattern in php « PHP Programming Language « IT news, forums, messages
Re: factory pattern in php

Posted by Jacob Atzen on 11/15/79 11:22

On 2005-07-26, Michael G <mike-g@montana.com> wrote:
> I want to use a factory pattern to create a variety of objects. Which object
> gets created depends on a string that is passed to the factory create
> method. I would like to avoid using an if or switch construct to accomplish
> this but not sure how. For example,
>
> ...
>
> if($nextObject == "login")
> {
> return new Login();
> }
> else if($nextObject == "validatedata")
> {
> return new Validate(aString);
> }
>
> ...
> Is there any way to avoid this?

You could do:

function create($className) {
return new $className;
}

Not sure if it's a better solution though.

--
Cheers,
- Jacob Atzen

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация