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
[Back to original message]
|