Re: Properly handling multiple constructors.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 24 March 2010 12:39, Peter Lind <peter.e.lind@xxxxxxxxx> wrote:
> And how exactly does that differ from building the same pizza in
> different ways? Builder doesn't mean you have to create different
> objects, it means taking the complexity in building a given object or
> set of objects and storing it in one place.
>
> In your case, it allows you to build your object in different ways
> while documenting it properly and avoid the huge switch inside your
> constructor that Nilesh proposed.
>
> On 24 March 2010 13:35, Richard Quadling <rquadling@xxxxxxxxxxxxxx> wrote:
>> On 24 March 2010 12:06, Peter Lind <peter.e.lind@xxxxxxxxx> wrote:
>>> Hmmm, that looks to me like you're trying to solve a problem in PHP
>>> with a c/c++c/# overloading solution. I'd give the builder pattern a
>>> try instead: http://en.wikipedia.org/wiki/Builder_pattern
>>>
>>> On 24 March 2010 13:01, Richard Quadling <rquadling@xxxxxxxxxxxxxx> wrote:
>>>> Hi.
>>>>
>>>> I have a scenario where I would _like_ to have multiple constructors
>>>> for a class.
>>>>
>>>> Each constructor has a greater number of parameters than the previous one.
>>>>
>>>> e.g.
>>>>
>>>> <?php
>>>> class myClass {
>>>> __construct(string $Key) // use key to get the complex details.
>>>> __construct(string $Part1, string $Part2, string $Part3) //
>>>> Alternative route to the complex details.
>>>> __construct(array $Complex) // All the details
>>>> }
>>>>
>>>> Essentially, SimpleKey is a key to a set of predefined rules. Part1, 2
>>>> and 3 are the main details and well documented defaults for the rest
>>>> of the rules. Complex is all the rules.
>>>>
>>>> Each constructor will end up with all the parts being known ($Key,
>>>> $Part1, $Part2, $Part3, $Complex).
>>>>
>>>> But, PHP doesn't support multiple constructors.
>>>>
>>>> Initially I thought about this ...
>>>>
>>>> __construct($Key_Part1_Complex, $Part2=Null, $Part3=Null)
>>>>
>>>> But then documenting the first param as being 1 of three different
>>>> meanings is pretty much a no go.
>>>>
>>>> So I'm looking for a clean and easily understood way to provide this.
>>>>
>>>> I won't be the only user of the code and not everyone has the same
>>>> knowledge level, hence a mechanism that is easily documentable.
>>>>
>>>> I think I may need a factory with multiple methods (FactoryKey,
>>>> FactoryPart1To3, FactoryComplex). Make the factory a static/singleton.
>>>> All these methods eventually call the real class with the complex
>>>> rule.
>>>>
>>>> Is that obvious enough?
>>>>
>>>> Regards,
>>>>
>>>> Richard.
>>>>
>>>>
>>>> --
>>>> -----
>>>> Richard Quadling
>>>> "Standing on the shoulders of some very clever giants!"
>>>> EE : http://www.experts-exchange.com/M_248814.html
>>>> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
>>>> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
>>>> ZOPA : http://uk.zopa.com/member/RQuadling
>>>>
>>>> --
>>>> PHP General Mailing List (http://www.php.net/)
>>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> <hype>
>>> WWW: http://plphp.dk / http://plind.dk
>>> LinkedIn: http://www.linkedin.com/in/plind
>>> Flickr: http://www.flickr.com/photos/fake51
>>> BeWelcome: Fake51
>>> Couchsurfing: Fake51
>>> </hype>
>>>
>>
>> I'm not building different types of "pizza". Just the same pizza via
>> different routes.
>>
>> Along the lines of ...
>>
>> Pizza = new Pizza('MyFavouritePizza') // A ham+pineapple+cheese pizza.
>> Pizza = new Pizza('ham', 'pineapple', 'cheese'); // A generic
>> ham+pineapple+cheese pizza
>> Pizza = new Pizza(array('base' => 'thin', 'toppings' => array('ham',
>> 'pineapple'), 'cheese'=>true)); // A complex description.
>>
>> I suppose the interfaces are beginner, intermediate and advanced, but
>> ultimately all generate identical objects.
>>
>> Richard.
>>
>> --
>> -----
>> Richard Quadling
>> "Standing on the shoulders of some very clever giants!"
>> EE : http://www.experts-exchange.com/M_248814.html
>> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
>> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
>> ZOPA : http://uk.zopa.com/member/RQuadling
>>
>
>
>
> --
> <hype>
> WWW: http://plphp.dk / http://plind.dk
> LinkedIn: http://www.linkedin.com/in/plind
> Flickr: http://www.flickr.com/photos/fake51
> BeWelcome: Fake51
> Couchsurfing: Fake51
> </hype>
>

Aha.

light bulb moment.

Thank you.


-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux