php - Best practice with classes -


'i have question best way handle class properties.

for ease of explanation, lets have class called company. "company" has single properties, such "name", "address" etc. in addition these single properties, "company" has multiple employees, multiple offices, & multiple coffee machines. (bad example, best think of).

when initializing class, can run sql query in constructor method retrieve name, address etc. however, "employees", "offices" , "coffee machines" stored in separate database tables, , return multiple results, instantly set properties need run 3 more sql queries.

is best way go, or best practice create 3 methods "getemployees", "getoffices" & "getcoffeemachines" , run queries when needed?

not sure if clear or not. here 2 examples. best this, thereby calling 4 sql queries on initialisation information instantly available:

class company {     private $name;     private $address;     private $employees;     private $offices;     private $coffeemachines;      public function __construct()     {         $this->employees = array();         $this->offices = array();         $this->coffeemachines = array();          ... sql name , address ...         $this->name = $rs['name'];         $this->address = $rs['address'];          ... sql employees ...         while ($rs = mysql_fetch_array)         {             $this->employees[$rs['id']] = $rs['name'];         }          ... sql offices ...         while ($rs = mysql_fetch_array)         {             $this->offices[$rs['id']] = $rs['office'];         }          ... sql coffee machines ...         while ($rs = mysql_fetch_array)         {             $this->coffeemachines[$rs['id']] = $rs['coffeemachine'];         }     } } 

or best this, run 1 sql query on initialisation , run future queries when needed

class company {     private $name;     private $address;     private $employees;     private $offices;     private $coffeemachines;      public function __construct()     {         ... sql name , address ...         $this->name = $rs['name'];         $this->address = $rs['address'];     }      public function getemployees()     {         ... sql employees ...         while ($rs = mysql_fetch_array)         {             $this->employees[$rs['id']] = $rs['name'];         }     }      public function getoffices()     {         ... sql offices ...         while ($rs = mysql_fetch_array)         {             $this->offices[$rs['id']] = $rs['office'];         }     }      public function getcoffeemachines()     {         ... sql coffee machines ...         while ($rs = mysql_fetch_array)         {             $this->coffeemachines[$rs['id']] = $rs['coffeemachine'];         }     } } 

for worth, suspect latter, use other opinions.

thanks

i think it's matter of preference , performance. if can read child collections acceptable startup performance, choose that. lazy initialization load more @ start, need protect against trying populate collections multiple times.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -