1

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!"
?>

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You're welcome, I'm glad I helped. If you want a cleaner solution (without an auxiliary variable), please let me know and I'll edit the answer to include that solution also.
oh that would be awesome too, I'd love to see it
I've added the cleaner solution. It should use less RAM that the first solution, because of the removal of the temporary lc variable. On another note, you should make your class variables private or protected, as you already have get and set methods (at least for the $lc variable).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.