|
Posted by Justin Koivisto on 10/31/72 11:22
Michael G wrote:
> "Michael G" <mike-g@montana.com> wrote in message
> news:42e6a0f3$1_1@spool9-west.superfeed.net...
>
>>"Jacob Atzen" <jacob@aub.dk> wrote in message
>>news:slrndecvlr.a1s.jacob@tank.aub.dk...
>>
>>>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.
>>>
>>
>>That's not a bad idea. It would eliminate the need for an array of
>>strings.
>
> Just a note, some languages would not allow that. That makes me a bit
> uncomfortable. They would require an explicit open/closed paran at a
> minimum.
This is what you be considered a variable function. Best practice is to
include the parenthesis like:
$className='myClass';
$a = new $className();
--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
Navigation:
[Reply to this message]
|