I have worked with oop, but this is my first project implementing it myself. Class getData returns an array populated with items from a specific .txt doc, based on what arguments are fed to it. Another class, WantsAndsNeeds, pulls in values a an argument, and for each one, instantiates an instance of the getData object to pull in data for use on the page. Here is my code and problem:
public function __construct($types) {
$models = array();
//dynamically instantiate data objects and retrieve data
if($types) {
$loadThese = explode(",",$types);
foreach($loadThese as $loadThis) {
$name = (string)$loadThis;
$$name = array();
$tempObjName = $loadThis.'_obj';
$$tempOhbjName = new loadData($loadThis);
$$name = $$tempObjName->returnData();
}
}
print_r($needs);
}
will return an array from a text document with the same name as the argument fed to it. This block, however, returns an error:
public function __construct($types) {
$models = array();
//dynamically instantiate data objects and retrieve data
if($types) {
$loadThese = explode(",",$types);
foreach($loadThese as $loadThis) {
$name = (string)$loadThis;
$$name = array();
$tempObjName = $loadThis.'_obj';
$$tempOhbjName = new loadData($loadThis);
$$name = $$tempObjName->returnData();
}
}
}
print_r($needs);
I think its a scope issue but have googled and tried a lot of different things, with no luck yet. Here is the code for class loadData:
class loadData {
//vars
protected $data = array();
// constructor
public function __construct($type) {
$raw_data = file_get_contents(dirname(dirname(__FILE__)).'/data/'.$type.'.txt');
$raw_entries = explode("|", $raw_data);
$raw_data = file_get_contents(dirname(dirname(__FILE__)).'/data/'.$type.'.txt');
$raw_entries = explode("|", $raw_data);
//check if return should be array or multidimensional array, then build
if (strpos($raw_entries[0],',') !== false) {
//if multidimensional array
$innerArray = array();
foreach($raw_entries as $item) {
$raw_multi = explode(",",$item);
$count = count($raw_multi);
for ($x=1; $x<$count; $x++) {
$innerArray[] = $raw_multi[$x];
}
$this->data[] = array($raw_multi[0],$innerArray);
$pos++;
$innerArray = array();
}
//if not multidimensional array
} else {
foreach($raw_entries as $item) {
$this->data[] = $item;
}
}
}
// send data
function returnData() {
return $this->data;
}
}
Can anybody see any solutions, or a better way to do this?