lundi 23 mars 2015

Pass object or id to a Model class?

Let say you have a model class Blog, and has a method getPostsByAuthor



function getPostsByAuthor($user) {
..
}

$user = User::getById($user_id);
$posts = Blog::getPostsByAuthor($user);


Assume $user is another Model class called User, there is another way to write this method, e.g.



function getPostsByAuthor($user_id) {
$user = User::getById($user_id);
...
}

$posts = Blog::getPostsByAuthor($user_id);
// or if $user already exists
$posts = Blog::getPostsByAuthor($user->getId());


It seems to me the 2nd way of writing lead to a more re-usable code and it preferred? Right?


Aucun commentaire:

Enregistrer un commentaire