Skip to content

Instantly share code, notes, and snippets.

@andyhmltn
Last active December 14, 2015 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyhmltn/5055595 to your computer and use it in GitHub Desktop.
Save andyhmltn/5055595 to your computer and use it in GitHub Desktop.
This is a proof of concept of a database class for PHP
<?
class MySQLAdapter {
private $sql;
public function run_query()
{
die($this->sql());
}
public function sql($sql = null)
{
if ($sql == null) return $this->sql;
if ($this->sql != null) $this->sql .= " ".$sql;
else $this->sql = $sql;
}
public function select($columns)
{
$this->sql("SELECT $columns");
return $this;
}
public function where($arguments = array())
{
$end_sql = 'WHERE 1=1';
foreach ($arguments as $column => $value)
{
$end_sql .= " AND $column = $value";
}
$this->sql($end_sql);
return $this;
}
public function order_by($column, $order = 'DESC')
{
$this->sql("ORDER BY $column $order");
}
}
class Database {
private $adapter;
private $id_column = 'id';
public function __construct($adapter)
{
$this->adapter = $adapter;
}
public function find($id)
{
$this->adapter->select('user_id')->where(array($this->id_column => $id))->order_by('id', 'DESC');
return $this->adapter->run_query();
}
}
$db = new Database( new MySQLAdapter );
$db->find(5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment