Code: Select all
class Utilities{
function __construct(){
global $db;
$this->db =& $db;
}
// GET function for all properties
function get($property){
global $$property;
return $this->$property;
}
// SET function for all properties
function set($property, $value){
global $$property;
if (is_object($$property)){
// incomplete
}else{
$this->$property = $value;
}
}
}
Code: Select all
class Thing extends Utilities{
// GET id in constuct and build thing
// (if this is a new object, be sure to pass 0 to $thingId)
public function __construct($thingId, $newArray = array()){
parent::__construct();
if($thingId > 0){
$this->set('thing', $thingId);
$query = "SELECT * FROM thing WHERE id = '".$thingId."'";
$dataArray = $db->query($query)->fetch_assoc();
// CREATE variables from data
foreach($dataArray as $k=>$v){
$this->set($k, $v);
}
return $thingId;
}else{
// we're going to make a new one.
// CREATE the data table
$this->db->insert('thing', $newArray);
foreach($newArray as $k=>$v){
//parent::set($k, $v);
$this->set($k, $v);
}
// RETURN the new id
$this->set('thing', $this->db->insert_id);
return $this->db->insert_id;
}
}
public function save(){
// SAVE all portions that are part of Thing
$dataArray = array('additionalType' => $this->get('additionalType'), 'description' => $this->get('description'), 'image' => $this->get('image'), 'name' => $this->get('name'));
$this->db->update('thing', $dataArray, 'id = "'.$this->get('thing').'"');
return $this->get('thing');
}
}