Example #1
0
 private function compile()
 {
     $lines = getProductFileLines($this->file);
     foreach ($lines as $line) {
         $id = getIDFromLine($line);
         $name = getNameFromLine($line);
         $this->products[$id] = getProductObjectFromID($id, $name);
     }
 }
Example #2
0
{
    if (preg_match("/.*-(.*)\\s\\d+/", $line, $array)) {
        return str_replace('_', ' ', $array[1]);
    }
    return '';
}
function getIDFromLine($line)
{
    if (preg_match("/^(\\d{1,3})-/", $line, $array)) {
        return $array[1];
    }
    return -1;
}
class Product
{
    public $id;
    public $name;
    function __construct($id, $name)
    {
        $this->id = $id;
        $this->name = $name;
    }
}
$lines = getProductFileLines('test.txt');
$objects = array();
foreach ($lines as $line) {
    $id = getIDFromLine($line);
    $name = getNameFromLine($line);
    $objects[$id] = getProductObjectFromID($id, $name);
}
print_r($objects);