doutanghuan9595
2014-04-03 09:04
浏览 42
已采纳

PHP类应该如何与MySQL数据库交互?

The other day, while developing my PHP project and implementing the User class, i started to wonder how this class should interact with the MySQL database the project is using.

Let me start with an example: let's say I have a getName() method, inside the User class, that returns the user's real name. What's the more fitting way to implement that method?

I came up with 2 solutions:

  1. I put the DB query inside the getName() and only get what I need like this:

    public function getName() {
    
        // MySQL query code here
    }
    
  2. I create a load() method inside the User class that load all the user data inside the class structure and then the getName() is something like this:

    private $name;
    
    // Load all user data inside the class' structure
    public function load() {
    
        $this->name = // MySQL query here
    }
    
    public function getName() {
    
        return $this->name;
    }
    

I thought, not sure if mistakenly or not, that the first way is more efficient because i only get the data I need, while the second way is more OOP but less efficient.

So, here's the question: what is the better way? Why? Are there better ways?

图片转代码服务由CSDN问答提供 功能建议

前几天,在开发我的PHP项目并实现 User 类时,我开始了 想知道这个类应该如何与项目正在使用的MySQL数据库进行交互。

让我先举一个例子:假设我有一个 getName()方法 ,在 User 类中,返回用户的真实姓名。 实现该方法的更合适的方法是什么?

我想出了两个解决方案:

  1. 我放了 在 getName()中的数据库查询,只得到我需要的东西:

      public function getName(){
     
     // MySQL 查询代码
    } 
        
     
  2. 我在 User 中创建 load()方法 code>类加载类结构中的所有用户数据,然后 getName()是这样的:

      private $ name; \  n 
     //在类的结构中加载所有用户数据
    公共函数load(){
     
     $ this-> name = // MySQL查询
    } 
     
    公共函数getName(){\  n 
    返回$ this-> name; 
    } 
        
      
     
     

    我想,不确定是否错误 ,第一种方式更有效,因为我只得到我需要的数据,而第二种方式是更多的OOP但效率更低。

    所以,这里有一个问题:什么是更好的方法 ? 为什么? 有更好的方法吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doujingdai5521 2014-04-14 07:03
    已采纳

    Either way, consider storing/caching the results of that so you do not make a query every time you use getName on that object. Also, consider not wrrying about all that by using a ORM/DBAL Solution like propel or doctrine.

    Also check out Lazy Loading and the Active Record Pattern

    点赞 打赏 评论
  • dpmrakfbx820320638 2014-04-03 09:09

    Run your query just in time and only run it once (unless you know the value might change), try something like the following:

     class User {
       protected $data;
    
       function getName()
       {
         if (!isset($data['name'])) {
           // if you can load more than just $this->data['name'] in one query
           // you probably should.
           $this->data['name'] = // query here
         }
         return $this->data['name'];
       }
    
     }
    
    点赞 打赏 评论
  • dougu1952 2014-04-03 09:10

    Aside from the question being kinda broad (as there are countless patterns), the second way you mentioned is better IMO, and to add to it I would also suggest supplying ID as a parameter which you could then use to build a single query to fetch the user by ID and then manually assign all properties (from the fetched row).

    点赞 打赏 评论

相关推荐 更多相似问题