重新创建PHP对象和类

时间:2014-11-23 18:16:09

标签: php class variables object

我有点难过。我从来没有在PHP中过多地讨论过对象和类,但有人建议我用它重新编写一些代码。
我想要做的是让$ auction成为一个对象属性,同时将所有行数据保存到它。

现在,我echo $auctions[1]['title'];回应列出id = 1 title的列表。 我希望重新创建它,以便它成为一个对象。

这是我目前的代码,

$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'];
    }
}

我对如何实现这一目标一无所知,但如果有人能给我一点指示,指出我的方向,我将非常感激! :)

5 个答案:

答案 0 :(得分:2)

创建一个类auctions,其中包含您在上面列出的所有必需成员变量(例如idtitlefeature_image等。接下来,在类中创建一个可以接受setValues()的setter方法(例如$row)。

$sqlquery = "SELECT * FROM auctions";
$auction = new Auctions();
if ($result = $db->query($sqlquery)) {
    while ($row = $result->fetch_assoc()) {

       $auction->setValues( $row );
       // do something with $auction...
    }
}

您也可以使用魔术方法__set()

,而不是显式的setter方法

答案 1 :(得分:1)

我现在在这里写一个最小的片段:

首先让我们为所有模型创建一个基类:

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

接下来,创建一些具体的模型:

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

我们现在可以使用它了:

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

您可以直接向User模型添加一些特定于用户的方法(也称为登录)。

此外,您应该实现其他模型方法,例如getByIdgetByQuerysave等,并且不使用直接sql查询,因为模型可以自己执行此操作

答案 2 :(得分:0)

您可以将值存储在

之类的对象中
$obj = new stdClass; //create new standard class object

$obj->id = $row['id']; //assign property value
$obj->title = $row['title'];
//further properties

......等等

答案 3 :(得分:0)

你真的想要创建一个对象数组(包含一次拍卖信息的类型的实例。像这样:

class Auction
{
    var $id = null;
    var $title = null;
    var $featured_image = null;
    var $description = null;
    var $date = null;
    var $location = null;
    var $highlights = null;
    var $catagories = null;
    var $notes = null;
    var $terms = null;
    var $contact = null;
}

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

请注意,您有拼写错误的"类别" (你有" catagories")。

答案 4 :(得分:0)

我建议您使用PDO

class Auction
{
    public $id;
    public $title;
    public $featured_image;
    public $description;
    public $date;
    public $location;
    public $highlights;
    public $catagories;
    public $notes;
    public $terms;
    public $contact;

    // This will return $all being an array of objects of class Auction
    public static function getAll() {
        $query = "SELECT * FROM auctions";

        $statement = $db->prepare($query);
        if (!$statement->execute())
            return false;
        $all = $statement->fetchAll(PDO::FETCH_CLASS, "Auction");
        return $all;
    }
}