dbillings Posted June 20, 2008 Share Posted June 20, 2008 Hi all, I'm trying to pass an array of objects as a parameter in a method. I keep receiving the error Fatal error: Cannot use object of type Text as array in C:\wamp\www\clan\Form.php on line 22 Any help would be appreciated. <?php #Form Class 6/20/08 #Last Edit class Form { var $data; public function Form ($name, $id, $method, $action) { $this->data['name']= $name; $this->data['id']= $id; $this->data['method']= $method; $this->data['action']= $action; } public function create_Form($inputs) { $i=0; while($inputs){ echo" <label>{$inputs[$i]->text['label']}</label><input type='{$inputs[$i]->text['type']}' name='{$inputs[$i]->text['name']}'><br /> "; $i++; } } } Class Text extends Form { var $text; public function Text($label, $name, $id, $type, $value='NULL') { $this->text['label']= $label; $this->text['name']= $name; $this->text['id']= $id; $this->text['type']= $type; if($value != 'NULL'){ $this->text['value']= $value; } } } $form_name= 'Name'; $form_id= 'ID'; $form_method= 'post'; $form_action= $_SERVER['PHP_SELF']; $input_label= 'Name'; $input_name= 'name'; $input_id= 'ID'; $input_type= 'text'; $name_input= array(); $input_label='Name2'; $input_name= 'name2'; $input_id='ID2'; $input_type='text'; $registration_form= new Form($form_name, $form_id, $form_method, $form_action); $name_input= new Text($input_label, $input_name, $input_id, $input_type); $name_input= new Text($input_label2, $input_name2, $input_id2, $input_type2); $registration_form->create_Form($name_input); ?> Quote Link to comment https://forums.phpfreaks.com/topic/111187-solved-passing-an-array-of-objects-as-a-parameter-in-method/ Share on other sites More sharing options...
dbillings Posted June 21, 2008 Author Share Posted June 21, 2008 I've modified the code a bit I added an addInput field to my form class and just created the new input object in the form class. I still can't get my loop to print any of my object data to the page. Help! <?php #Form Class 6/20/08 #Last Edit class Form { var $data; var $input=array(); public function Form ($name, $id, $method, $action) { $this->data['name']= $name; $this->data['id']= $id; $this->data['method']= $method; $this->data['action']= $action; } public function add_Input($label, $name, $id, $type, $value='NULL') { $input[]= new Text($label, $name, $id, $type, $value); } public function create_Form() { foreach($this->input as $value){ echo $value->text['name']; } } } Class Text extends Form { var $text; public function Text($label, $name, $id, $type, $value='NULL') { $this->text['label']= $label; $this->text['name']= $name; $this->text['id']= $id; $this->text['type']= $type; if($value != 'NULL'){ $this->text['value']= $value; } } } $registration_form= new Form("Name", "ID", "post", $_SERVER['PHP_SELF']); $registration_form->add_Input("Name", "name", "ID", "text"); $registration_form->add_Input("Age", "age", "ID", "text"); $registration_form->create_Form(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/111187-solved-passing-an-array-of-objects-as-a-parameter-in-method/#findComment-570706 Share on other sites More sharing options...
Mastodont Posted June 21, 2008 Share Posted June 21, 2008 $input[]= new Text($label, $name, $id, $type, $value); should be $this->input[]= new Text($label, $name, $id, $type, $value); Quote Link to comment https://forums.phpfreaks.com/topic/111187-solved-passing-an-array-of-objects-as-a-parameter-in-method/#findComment-570898 Share on other sites More sharing options...
dbillings Posted June 21, 2008 Author Share Posted June 21, 2008 Thank you for that Mastodont. I'm still having trouble though it's throwing this error, Fatal error: Cannot access empty property in C:\wamp\www\clan\Form.php on line 38 I added a comment showing line 38 <?php #Form Class 6/20/08 #Last Edit Class Text { var $text; public function Text($label, $name, $id, $type, $value='NULL') { $this->text['label']= $label; $this->text['name']= $name; $this->text['id']= $id; $this->text['type']= $type; if($value != 'NULL'){ $this->text['value']= $value; } } } class Form { var $data; var $input=array(); public function Form ($name, $id, $method, $action) { $this->data['name']= $name; $this->data['id']= $id; $this->data['method']= $method; $this->data['action']= $action; } public function add_Input($label, $name, $id, $type, $value='NULL') { $this->$input= new Text($label, $name, $id, $type, $value); // Line 38 } public function create_Form() { foreach($this->input as $value){ echo $value->text['name']; } } } $registration_form= new Form("Name", "ID", "post", $_SERVER['PHP_SELF']); $registration_form->add_Input("Name", "name", "ID", "text"); $registration_form->add_Input("Age", "age", "ID", "text"); $registration_form->create_Form(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/111187-solved-passing-an-array-of-objects-as-a-parameter-in-method/#findComment-570930 Share on other sites More sharing options...
dbillings Posted June 21, 2008 Author Share Posted June 21, 2008 Syntax Error! Quote Link to comment https://forums.phpfreaks.com/topic/111187-solved-passing-an-array-of-objects-as-a-parameter-in-method/#findComment-570972 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.