Quote:
Originally Posted by Thyrosis
You can't store an object as such in a database (not as far as I'm aware anyway), but my first port of call is usually create a Class for every database-table.
|
You could through serialization however I would say you should almost never do it. It defeats the entire purpose of a relational database.
Quote:
Originally Posted by Thyrosis
So it would look like this:
PHP Code:
function get_data() {
$sql = "SELECT * FROM comments WHERE id='{$this->id}';";
$result = $this->query($sql);
while($r = $result->fetch_assoc()) {
$this->id = $r['id']
[...]
}
}
function set_data() {
$sql = "UPDATE comments SET commenter='{$this->commenter}' [..] WHERE id='{$this->id}';
$this->query($sql);
}
On the front end, the only thing you'll have to do is something like this.
PHP Code:
$comment = new Comment();
$comment->id = 1;
$comment->get_data(); // You could combine these by using $comment->get_data(1), obviously build in an if statement in get_data checking if $id is set or something)
$comment->commenter = 'Martin';
$comment->set_data();
Okay, granted, a bit useless for this particular example, but with an object with 20 variables or something it really pays off 
|
So do you validate the data as part of the class before the set_data call? E.g. to be sure the values don't contain an SQL injection attack?
---------------------------------------
As firepages has said earlier, OOP is a tool, the trick is to know when to use it and how to use it effectively. When you get into OOP it can be easy to object orientate yourself into a corner. You either under or over design your objects or worse start to create an inner platform effect with your software.