What should happen when I call $user->get_email_address()
?
Option 1: Pull the email address from the database on demand
public function get_email_address() {
if (!$this->email_address) {
$this->read_from_database('email_address');
}
return $this->email_address;
}
Option 2: Pull the email address (and the other User attributes) from the database on object creation
public function __construct(..., $id = 0) {
if ($id) {
$this->load_all_data_from_db($id);
}
}
public function get_email_address() {
return $this->email_address;
}
My basic question is whether it's best to minimize the number of database queries, or whether it's best to minimize the amount of data that gets transferred from the database.
Another possibility is that it's best to load the attributes that you'll need the most / contain the least data at object creation and everything else on demand.
A follow-up question: What do ORM abstraction frameworks like Activerecord do?