Jump to content

Passing a number of arguments on a sigle string


rastacre

Recommended Posts

Hi All,

 

I'm a newbe of the forum and of PHP in general.

 

I have a class items with no constructor but few functions that applies to it and most of the sub classes.

 

then i have classes extending it publisher, category, author and book. They both have constructors:

 

publisher and category, have the same constructor with one argument for $name

 

author' arguments are $name and $surname

 

and for book i have the following constructor:

 

__construct($category_id , $isbn , $name , $year , $author_id , $publisher_id , $price , $qty) 

 

now i'm trying to do this:

 

$type = 'book';
$attributes = "'1', 'qwerty123', 'The Title', '2000', '2', '3', '15.95', '12'";
$new_{"$type"} = new $type({$attributes});

 

i understand that

 

new $type({$attributes})

 

doesnt work, but i'd like to know if it's actually possible to pass the arguments with a single string interpreted not as the variable itself, but the variable's value....

 

i hope it makes sense the way i explained and I hope someone can shed some light on this... the PHP manual didn't help me too much.

 

Thanks in advance

 

RastaCre

Yeah, send it as an array. I kinda asked the same question before.

 

Just send it as an associative array and use variable variables

 

$arr = array('a' => 1, 'b' => 2);

 

then you just loop through and set the vars

 

foreach($arr as $key => $val){

$$key = $val;

}

 

that will give you

$a which holds the value 1 and $b which holds 2

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.