dtla92562 2014-11-23 18:16
浏览 37
已采纳

重新创建PHP对象和类

I'm a little bit stumped. I've never messed around with objects and classes too much in PHP, but someone recommended that I re-did some code with it.
What I'm trying to do is make $auctions an object property, while saving all of the row data to it.

Right now, I do echo $auctions[1]['title']; to echo out the listing where id=1 title. And I wish to re-create it so that it would be an object.

Here's my current code,

$sqlquery = "SELECT * FROM auctions";
if ($result = $db->query($sqlquery)) {
    while ($row = $result->fetch_assoc()) {
        $auctions[$row['id']]['id'] = $row['id'];
        $auctions[$row['id']]['title'] = $row['title'];
        $auctions[$row['id']]['featured_image'] = $row['featured_image'];
        $auctions[$row['id']]['description'] = $row['description'];
        $auctions[$row['id']]['date'] = $row['date'];
        $auctions[$row['id']]['location'] = $row['location'];
        $auctions[$row['id']]['highlights'] = $row['highlights'];
        $auctions[$row['id']]['catagories'] = $row['catagories'];
        $auctions[$row['id']]['notes'] = $row['notes'];
        $auctions[$row['id']]['terms'] = $row['terms'];
        $auctions[$row['id']]['contact'] = $row['contact'];
    }
}

I don't have any idea on how to accomplish this, but if someone could give me a little hint to point me in the direction, it would be very appreciated! :)

  • 写回答

5条回答 默认 最新

  • dongzz4545 2014-11-23 19:45
    关注

    I'll write a minimal snippet here now:

    First let create a base class for all our models:

    abstract class Model() {
      public $fields = array();
      private $data = array();
      public function setValues(array $vals) {
        foreach($vals as $key=>$value) {
          if (in_array($key, static::$fields)) {
            $this->data[$key] = $value;
          }
        }
      }
      public function get($key) {
          if (in_array($key, static::$fields) && isset($this->data[$key])) {
            return $this->data[$key];
          }
          return null; // or throw Exception)
      }
    }
    

    Next, create some concrete model:

    class Users extends Model {
      public static $fields = array('id', 'name');
    }
    

    And we can use it now:

    $users = array();
    $sqlquery = "SELECT * FROM Users";
    if ($result = $db->query($sqlquery)) {
        while ($row = $result->fetch_assoc()) {
          $user = new User();
          $user->setValues($row);
          $users[] = $user;
        }
    }
    

    You can to add some user-specific methods (aka login) to User model directly..

    Also you should to implement other Model methods, like getById, getByQuery, save and other, and no use direct sql queries, because models can do this itself

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

报告相同问题?

悬赏问题

  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'