开发者

OOP or MySQL Calls. Make object or call directly from MySQL?

I'm not sure what's a better practice or a more real-world practice. I'm looking to create a Catalog System from scratch, but unsure what the best approach would be.

I was thinking that I use objects when I need to display information like, info.php?id=100. Have code like this for displaying

Game.class.php file

class Game {
    private $name;
    private $type;
    private $database;

    public function __construct($database, $id) {
        $information = $database->select($id); // returns mysql_fetch_array

        $this->database = $database;
        $this->setName($information['name']);
        $this->setType($information['type']);
    }

    public function __destruct() {
        unset($this->name);
        ...
    }
    ...
    private function setName($name) {
        $this->name = $name;
    }
    ...
    public function getName() {
        return $this->name;
    }
    ...
    public function addToDatabase() {
        // stuff here to check if it exists in the database or not
        $database->insert('test', array('id' => '', 'name' => $this->name, 'type' => $this->type));
    }
}

info.php?id=100 file

// already have __autload()

$id = sanitize($_POST['id']);
$item = new Game($db, $id);
echo "Item name: " . $item->getName() . " <br />";

But I don't really need to create objects for like when I add or update things add.php?name=XMen&type=GameBoy

I wouldn't want to do this:

$name = sanitize($_POST['name']); // sanitize
$type = sanitize($_POST['type']); // sanitize
$newObject = new Game($name, $type);
$newObject->addToDatabase();

I should instead I should just skip creating an object and just insert directly

$name = sanitize($_POST['name']); // sanitize
$type = sanitize($_POST['type']); // sanitize
$sql = "INSERT INTO test ('id', 'name', 'type') VALUES ('', $name, $type)"开发者_运维百科;
mysql_query($sql);

or if I had a database class

$name = sanitize($_POST['name']); // sanitize
$type = sanitize($_POST['type']); // sanitize
$db->insert('test', array('id' => '', 'name' => $name, 'type' => $type));


To get complete abstraction you might look into 3rd-party ORMs. At the very, very least you should create your own database class. I usually use a Singleton design pattern if I only plan on having one database connection.

You don't want raw SQL statements all over the place because you may need to switch to and/or support different versions of MySQL or even a different database all together. It'd be easier to replace a single database management object than to fix all those queries throughout your code.

One of my database classes is much like that third option you posted and it works quite well. It has a bunch of conveniences like insertArray(), getOneRow(), getOne(), getOneFromAllRows(), executeExternalFile(), and an iteratable result set for all SELECT-like queries.


For this example, it doesn't seem necessary to create the object first. Just insert straight into the database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜