Created
November 11, 2016 14:39
-
-
Save feketegy/0e6abf8c4ce20eea44b10f72cf7b5157 to your computer and use it in GitHub Desktop.
MVC + Repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyModel | |
{ | |
public $id; | |
public $name; | |
public $date_of_birth; | |
public function getDateOfBirth() | |
{ | |
return date('Y-m-d H:i:s', $this->date_of_birth); | |
} | |
} | |
class MyRepo | |
{ | |
private $_db; | |
private $_model; | |
public function __construct( $db, $model ) | |
{ | |
$this->_db = $db; | |
$this->_model = $model; | |
} | |
public function getMyData() | |
{ | |
try | |
{ | |
$data = $this->_db->executeQuery('SELECT * FROM my_table'); | |
$this->_model->id = $data[0]['user_id']; | |
$this->_model->name = $data[0]['full_name']; | |
$this->model->date_of_birth = $data[0]['dob']; | |
return true; | |
} | |
catch (DBException $e) | |
{ | |
return false; | |
} | |
} | |
} | |
class MyController | |
{ | |
$myModel = new MyModel(); | |
$repo = new MyRepo( $db, $myModel ); | |
$repo->getMyData(); | |
$view = new MyView( $myModel ); | |
$view->render(); | |
} | |
class MyView | |
{ | |
private $_model; | |
public function __construct( $model ) | |
{ | |
$this->_model = $model; | |
} | |
public function render() | |
{ | |
// Print the shit out of myModel | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment