rastacre Posted January 13, 2008 Share Posted January 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/85825-passing-a-number-of-arguments-on-a-sigle-string/ Share on other sites More sharing options...
trq Posted January 13, 2008 Share Posted January 13, 2008 No, its not possible to do it the way you have described. You would be best off sending your arguments within an array. Quote Link to comment https://forums.phpfreaks.com/topic/85825-passing-a-number-of-arguments-on-a-sigle-string/#findComment-438415 Share on other sites More sharing options...
emehrkay Posted January 14, 2008 Share Posted January 14, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/85825-passing-a-number-of-arguments-on-a-sigle-string/#findComment-438490 Share on other sites More sharing options...
rastacre Posted January 14, 2008 Author Share Posted January 14, 2008 Thanks guys, and emehrkay, thanks for the code, i thought that was the way to go, but i was still hoping for the string solution.. well I'll have to modify the rest of my classes as well thanks again Quote Link to comment https://forums.phpfreaks.com/topic/85825-passing-a-number-of-arguments-on-a-sigle-string/#findComment-438634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.