"/ Assume there is some record set $carset / "
This is kinda my initial question: isn't it the purpose that the cars-class generates this recordset? Or should every page using the cars class write it's own query ?
I was thinking of the following
class cars {
var $cars = array();
function cars (&$db) {
// where $db is an instance of my DBA layer ezSQL
// dunno how this could be improved but I don't feel
// at ease with this kind of passing around pointers
$this->db = $db;
}
function add(&$car) {
$fields = get_object_vars($car);
foreach ($fields as $field) {
$values[$field] = $car->$field;
}
$sql = "insert into cars (".implode (',', $fields).") values ('".implode ("','",$values)."')" ;
$this->db->query($sql);
$this->cars[] = $car;
}
funciton fetch_all() {
$sql = "select * from cars";
$this->cars = $this->db->get_results($sql);
}
function list_all() {
if (empty($this->cars)) {
$this->fetch_all();
}
return $this->cars;
}
}
So ... I would like to include my SQL statements in my classes. Is that a bad idea ? Should I set it up differently ?
Questions one never finds an answer to in articles around the internet. Or maybe I'm looking in the wrong place / for the wrong keywords