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条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像