Jump to content

[SOLVED] Passing an array of objects as a parameter in method


dbillings

Recommended Posts

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);

?>

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();

?>

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();

?>

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.