dpl22899 2011-11-09 19:21
浏览 52
已采纳

功能抽象

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 ?

  • 写回答

3条回答 默认 最新

  • dtwbp26022 2012-05-02 13:52
    关注

    Whilst cleaning up my account i thought it best to close some old questions. What i found is pretty obvious and i had a feeling i knew it all along. For anyone else that is wondering the same as i was and find's this post. The basic premise is it's your decision.

    Basically you can fully abstract your methods so their completely standalone and suffer some pretty major performance issues, or at the other end of the scale you can not abstract at all and be able to tweak every little thing to get the best performance. Performance and abstraction in a general sense opposite's, so find somewhere in the middle that makes you happy.

    In the case above i decided to make the method static then pass in either a user id, or an array. The code then checks if its an array or integer, if its an integer or if the array doesn't contain the required keys, it does a query lookup before generating a username for return.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答