douwei3863 2011-01-19 05:28
浏览 24
已采纳

哪个更好的PHP类查询?

I have PHP class that contain function for query data from database. The first one is

class x{

  public $var1;
  public $var2;

  function __construct(){ }

  function getX($primkey){
    $sql = mysql_qyery("SELECT * FROM x_table WHERE primkey='$primkey'");
    $row = mysq;_fetch_assoc($sql);
    $this->var1 = $row['var1'];
    $this->var2 = $row['var2'];
  }

}

and another on is

class x{

  public $var1;
  public $var2;

  function __construct(){ }

  function getX($primkey){
    $sql = mysql_qyery("SELECT * FROM x_table WHERE primkey='$primkey'");
    $row = mysq;_fetch_assoc($sql);
    return $row;
  }

}

two class has some difference on function getX(). The first class put data from database to class variables but the second one return whole data from database via array.

I would like to know about performance of two class, which one is better. and which one is good for programming or any idea?

Regards.

  • 写回答

3条回答 默认 最新

  • douya7121 2011-01-19 05:36
    关注

    With the first form, you have the flexibility to do additional conversions and asymmetrical mappings. So in that sense, you're truly creating a Model class that does an abstract representation of your actual data source.

    With the second form, if you follow the Convention Over Configuration (CoC) paradigm and clearly define your conventions, it may work. But, down the line, you may run into situations where you'll have to make exceptions. I would recommend the more flexible approach above, as an anticipation of future exceptions.

    Tangential: In case you're not writing your own object mappings purely for educational purposes, or for some other plausible reason, have you looked at RedBeanPHP or other alternatives?

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

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?