Re: Properly handling multiple constructors.

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

 



On 24 March 2010 12:08, Nilesh Govindarajan <lists@xxxxxxxxxx> wrote:
> On 03/24/2010 05:31 PM, Richard Quadling 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.
>>
>>
>
> Don't give specify any parameters in the function declaration.
>
> Use helper functions inside the class, and decide at the constructor which
> helper to call using func_get_arg() and func_get_args()
>
> You can get the no. of arguments using count(func_get_args())
>
> and then using swtich statement call the relevant helper function using
> call_user_func_array with parameters.
>
> A sample:
>
> public function __construct() {
>
> switch(count(func_get_args())) {
>
> case 1:
> call_user_func_array(array($this, '_helper1'), func_get_args());
> break;
> // and so on
> }
>
> }
>
> --
> Nilesh Govindarajan
> Site & Server Administrator
> www.itech7.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

And how would you docblock that?

Documentation is important as that describes the intent to the users
of the code.

-- 
-----
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