Reputation: 299
I have a multiple files that contains a string. I use a text delimiter to break the string into lines. I use another text delimiter to break each line into fields
I am using multiple classes to perform do this. I have a class (the line class) that I need to instantiate many times so I want an array of this class. I have hit a snag as I am getting a message that says I cannot use the object as an array. can you provide any suggestions. Here is the error message and my code
Fatal error: Cannot use object of type lineController as array in - on line 20`
<?php
require_once('/super_src/controller/fileController.php');
require_once('/super_src/controller/lineController.php');
require_once('/super_src/controller/elementController.php');
require_once('/super_src/controller/exceptionController.php');
class masterControl{
public $fc;//fileControl variable
public $lc = array();//lineControl variable
public $ec;//elementControl variable
public $xc;//exceptionControl variable
public function __construct(){
$this->fc = new fileController($this->path);
}
public function setLC($lc){$this->lc = $lc;}//end setLC()
//I get the error on this line where I have lc[$index]
public function setLCAtIndex($value, $index){$this->lc[$index] = $value;}//end setLCAtIndex()
public function getLC(){if($this->lc == null) return "";else return $this->lc;}//end getLC()
public function getLCAtIndex($index){if($this->lc == null && $this->lc != 0) return "";else return $this->lc[$index];}//end getLCAtIndex()
public function ediTeardown(){
$this->fc->searchFiles($this->fc->getPath());//the files
// var_dump($this->fc->getFile());
$index = 0;
foreach($this->fc->getFile() as $file){
$this->lc = new lineController();
$this->lc->extractLines($file);
$this->setLCAtIndex($file, $index);
$index++;
}//end foreach()
}//end ediTeardown()
public function echoArray($array){foreach ($array as $a){echo $a."-";echo"<br>";};}
public function __toString(){}
}
$mc = new masterControl();
$mc->ediTeardown();
//var_dump($mc->getLC());
echo "<br><br><br><br><br><br>End Of Line!"
?>
Upvotes: 1
Views: 82
Reputation: 131
On line 30 of your code you have
$this->lc = new lineController();
which is changing your "$this->lc" variable from array to object.
If you want to create an array of lineController's you should change that line to:
$tmp_lc = new lineController();
$tmp_lc->extractLines($file);
$this->lc[] = $tmp_lc;
EDIT:
For a much cleaner solution your code should look like this:
$num_lc = 0;
foreach($this->fc->getFile() as $file){
$this->lc[$num_lc] = new lineController();
$this->lc[$num_lc]->extractLines($file);
$this->setLCAtIndex($file, $index);
$index++;
$num_lc++;
}//end foreach()
With this code you don't need to set a temporary variable with a large structure, you use a counter for the array position, and write to the array directly.
Upvotes: 3