I'm hoping to do my next project using objects...
Imagine I have a database table called Tests, and I wanted to echo everything in it.
Originally I would have done this:
Code: Select all
$query = mysql_query("SELECT * FROM Tests");
while ($row = mysql_fetch_object($query)) {
echo $row->name, ', ', $row->address, "\n<br />";
}Code: Select all
<?php
class Test {
private $id, $name, $address;
public function get_id() {return $this->id;}
public function get_name() {return $this->name;}
public function get_address() {return $this->address;}
public function set_name($name) {$this->name = $name;}
public function set_address($address) {$this->address = $address;}
public function load_test($test_id) {
$query = mysql_query("SELECT * FROM Tests WHERE id = '$test_id'");
while ($row = mysql_fetch_object($query)) {
$this->id = $row->id;
$this->name = $row->name;
$this->address = $row->address;
}
}
public function save_test() {}
public function delete_test() {}
}
$my_test = new Test();
$my_test->load_test(1);
echo $my_test->get_name(), ', ', $my_test->get_address(), "\n<br />";
?>Surely it can't be, because I can only access ONE Test at a time
All I want to do is display everything in the Test table
It seems daft retrieving the data from the table, and then making a New Test Object for each row!
Hope you can correct my vague understanding of oo