|
Posted by Kimmo Laine on 12/08/23 11:50
"AlexVN" <alexander.netkachev@gmail.com> wrote in message
news:1150392090.453141.112440@u72g2000cwu.googlegroups.com...
> Henk,
>
> Thank you for the great answer. You are right--I'm looking for a method
> of instantiating class with dynamic parameters, not creating. I
> understant the method you proposed, but what I would much rather see is
> a method of calling class constructor with dynamic number of
> parameters. I suspect that PHP does not have such method (since you, a
> guru, do not listed it here) and will try to create a couple of ifs for
> my case.
If I understood correctly what you mean, it is possible. I did something
like this once. I wanted to instantiate an object of a class which had an
arbitary number of columns. It was sort of a table container. All columns
had a certain length and I create the structure of the table in the
constructor... So to create a table with two columns, one 20 units wide and
another 40 units wide, I'd call the constructor $myTable = new table(20,40);
or just as well new table(100,20,30,5,12,40);
Here's the definition of the constructor:
public function __construct(){
if(func_num_args()){
foreach(func_get_args() as $arg){
$this->columns[] = $arg;
}
}
}
So as you see, the constructor has no parameters at all, I just catch all
that were passed to it with the func_get_args() function that returns a list
of all the parameters passed to the method, and walk it thru. All very
simple. Is this what you mean?
--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)
[Back to original message]
|