I've been working with PHP for few years. Till now i'm still confused about designing model classes in MVC model because somehow MVC model againsts OOP style.
In the MVC, model classes in charge of communicating with database. With the OOP, everything is designed as objects. If i have a website, and it has posts, members, they're objects.
So what is the correct way to design model class in MVC in the object oriented style? I've seen a lot of coding style. There're the some of them:
Pure MVC style
// model class has $db object for connection
class Post extends Model {
function getPost() {
// query post
return $postObjectInArray;
}
function deletePost()
}
MVC with OOP using $db
as singleton
class Post {
function __construct($id, $author) {
$this->id = $id;
$this->author = $author;
}
static function getPost() {
$db = Mysql::getInstance();
$post = $db->queryPost();
return new Post($post['id'], $post['author']);
}
}
Or using $db
from parent class
class Post extends Model {
function __construct($id, $author) {
$this->id = $id;
$this->author = $author;
}
function getPost() {
$post = $this->db->queryPost();
return new Post($post['id'], $post['author']);
}
}
And many more ways like separating CRUD into PostGateway
with all sql query, Post is purely handling object data ...
So what's the best way to design model class in MVC with OOP style?
Aucun commentaire:
Enregistrer un commentaire