Just a quick question regarding re-useable code.
Ive lately been trying to modularise my code. I'm not really good with OO PHP yet (im working towards getting my head around it all) but even if i started using OOP there are a number of scenarios like the one below, where i see an issue with abstracting my code.
So lets say i have a list of users that have posted a comment in a news article. When i get the list of the comments, all i have is the user id. Before i started trying to abstract functionaility, i would have just done something like
SELECT c.*, u.username FROM comments AS c LEFT JOIN users AS u ON c.uid = u.id WHERE c.news_id = 90
This would give me the comment along with the username of who posted it. Now in order to abstract out the retrival of the username (lets say if the username was null, i may want to return the concat of fname and sname) i could do something like this. (bare in mind its an example, i know in most cases username would be a required registration field, imagine im building a base system for developing new sites where the username could be something other than a string in the database).
function getUsername($uid) {
return /** the username from the database however i decide to get it **/
}
That would have to run at least 1 mysql query. If i have 100 comments on display, that could be 100 queries being run
In OOP i guess i would do something like
$user = new User($uid);
echo $user->getUsername();
But that would also require a query to load the user (and probably be slightly more resource intensive as a user class would get far more information on init to build the user object for all the possible methods to follow.
I know i could use memcache or something to speed things up. Even cache the results so subsequent getUsername() calls with the same uid doesnt require another query. But ni cant think of a solution that comes anywhere near to being as good as getting the username in my usual way (first code snippet).
Am i looking too deep into function abstraction and trying to over use OOP? I dont think so personally, I think its a perfectly valid requirement, and it would make sense to instantiate a user object if i were to be using OOP?
Just looking for some pointers really. Will abstraction functionaility always impact performance? Or am i just going about it the wrong way ?