|
Posted by AlexVN on 11/28/58 11:50
Kimmo,
Thank for for the answer, but what I mean is a little bit different. I
want to simplify the following construction:
function CreateClass($className) {
switch (func_num_args()) {
case 1:
return new $className();
case 2:
return new $className(func_get_arg(1));
case 3:
return new $className(func_get_arg(1), func_get_arg(2));
...
}
}
In other words, I want something like:
function CreateClass($className) {
return create_user_class_array($className, array_shift($a =
func_get_args()));
}
Where create_user_class_array is the "magic" method similar to
call_user_func_array, which, I hope, should exist and I just did not
find it... But now I almost sure that there is no such method.
Thanks,
Alexander
http://www.alexatnet.com/
Kimmo Laine wrote:
> "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]
|