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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.