It’s better to expose two different functions to the user of the API, but internally you can use the same code for both, if you think that better code reuse will be used.
For example: you define a generic getArticles (for internal use only)
articles getArticles($userId, $offset, $limit) {
// do searching while handling the different cases if $userId is set or not and the same for the offsets
}
then you define your public functions like
articles getUserArticles($id){
return getArticles($id, -1,-1);
}
articles getAllArticles($offset, $limit) {
return getArticles(-1, $offset, $limit);
}
NOTE. do this only if you think that there is a common code between the two functions. If there is no generic code, save them separately and there is no need for the generic getArticles function that I suggested.
I don't know if the above is valid PHP code, but I think the idea is clear.
source
share