Skip to content

Instantly share code, notes, and snippets.

@lgoldstien
Last active December 18, 2015 21:09
Show Gist options
  • Save lgoldstien/5845121 to your computer and use it in GitHub Desktop.
Save lgoldstien/5845121 to your computer and use it in GitHub Desktop.
SOLID - S - SRP tasks class
<?php
/**
* Tasks - SRP
* A task database class that does abide by SRP
* @package solid
* @author [email protected]
*/
class db {
/**
* @var connection
*/
private $connection;
/**
* __construct
*/
function __construct( $db_host, $db_user, $db_pass, $db_name ) {
/**
* Make the connection
*/
$this->connection = new mysqli( $db_host, $db_user, $db_pass, $db_name );
}
public function query($sql) {
// Run the query
$result = $this->connection->query( $sql );
// Return the result
return $result->fetch_all
}
}
class tasks {
/**
* @var db
*/
private $db;
/**
* __construct
*/
function __construct( $db_host, $db_user, $db_pass, $db_name ) {
// Instantiate the db class
$this->db = new db( $db_host, $db_user, $db_pass, $db_name );
}
/**
* list
* List all the tasks in the database
*/
public function list() {
// The SQL query
$sql = "select `name`, `duedate`, `priority`, `complete` from tasks";
// Return the array from the query
return $this->db->query( $sql );
}
}
$tasks = new tasks( 'localhost', 'username', 'password', 'tasks' );
$tasks_list = $tasks->list();
print_r( $tasks_list );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment