Skip to content

Instantly share code, notes, and snippets.

@josefnpat
Created July 23, 2013 01:07
Show Gist options
  • Save josefnpat/6059093 to your computer and use it in GitHub Desktop.
Save josefnpat/6059093 to your computer and use it in GitHub Desktop.
<?php
class database_abstraction_layer {
// Store the entire db in memory, but make sure it's inaccesable to the user.
private $db;
// This is the file that this DAL will operate on. In MySQL, this will be configs.
private $file;
// This is the construct function that is called when the dal object is created.
public function __construct($file){
// Store the file name in the object
$this->file = $file;
// Load the database
$this->db = $this->load();
}
// Load the database: PRIVATE
private function load(){
if (file_exists($this->file)) {
$dbraw = file_get_contents($this->file);
if($dbraw == ""){
die("You are reading from the file too quickly. Re-write this DAL with MySQL in mind.");
} else {
$database = unserialize($dbraw);
if($database === FALSE){
die("Data will not unserialize. (Database most likely corrupted)");
} else {
return $database;
}
}
} else {
echo "Info: `".$this->file."` file does not exist: initializing.\n";
return $this->init();
}
}
// Initilize the database. You will want to set up your structure for the game here!
// To reset the databse, simple delete the db.dat (or whatever $this->file is)
private function init() {
// New database object using stdClass
$database = new stdClass();
// Array of strings?
$database->colors= array("Blue","Green","Red","Yellow");
// A string itself?
$database->name = "The Magic Rainbow";
// The time that the db is initialized.
$database->inittime = time();
return $database;
}
// This is the destruct function that is called when the dal object is destroyed.
public function __destruct() {
$this->save();
}
// This function saves the DB to file.
private function save() {
file_put_contents($this->file,serialize($this->db));
}
// This is your first actually accessable function. Keep in mind that these functions need to logically
// equate to MySQL commands. e.g. if this function updated the number of potatues, it would be
// "UPDATE users SET potato = \"$value\" WHERE UID = \"$UID\"";
public function example_update_potato($value){
$this->db->potato = $value;
}
// This is another sample function, just so you can see what data is actually stored in your awesome database.
public function example_getAllTheData(){
return $this->db;
}
}
#!/usr/bin/php
<?php
// We include the Database Abstraction Layer library
require("dal.php");
// We create a new instance of it, thus automatically calling __construct
$dal = new database_abstraction_layer("db.dat");
// We call a sample function, in this case update potatoes to time.
$dal->example_update_potato( time() );
// Here we print out what's actually in the database. Keep in mind,
// functions like this are bad, as it's hard to translate this into a MySQL function!
print_r($dal->example_getAllTheData());
// This is the end of the script, so the __destruct is automatically called.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment