类的最佳做法
'我有一个关于处理类属性的最佳方法的问题。类的最佳做法
为了便于解释,可以说我有一个叫做公司的班级。 “公司”具有单一属性,如“姓名”,“地址”等。除了这些单一属性外,“公司”还拥有多名员工,多个办公室,多台咖啡机。 (糟糕的例子,但我能想到的最好的)。但是,由于“Employees”,“Offices”和“Coffee Machines”全部存储在单独的数据库表中,因此在初始化类时,我可以在构造方法中运行SQL查询以检索名称,地址等。并返回多个结果,立即设置我需要运行三个SQL查询的属性。
这是最好的方法,还是最好的做法是创建三个方法“getEmployees”,“getOffices”&“getCoffeeMachines”并在需要时运行查询?
不确定这是否清楚。这里有两个例子。难道最好要做到这一点,因此呼吁初始化四个SQL查询,因此,所有的信息是即时可用:
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 to get name and address ...
$this->name = $rs['name'];
$this->address = $rs['address'];
... SQL to get employees ...
while ($rs = mysql_fetch_array)
{
$this->employees[$rs['id']] = $rs['name'];
}
... SQL to get offices ...
while ($rs = mysql_fetch_array)
{
$this->offices[$rs['id']] = $rs['office'];
}
... SQL to get coffee machines ...
while ($rs = mysql_fetch_array)
{
$this->coffeeMachines[$rs['id']] = $rs['coffeeMachine'];
}
}
}
或者是它最好要做到这一点,只能运行在初始化一个SQL查询,并在需要时运行以后查询
Class Company
{
private $name;
private $address;
private $employees;
private $offices;
private $coffeeMachines;
public function __construct()
{
... SQL to get name and address ...
$this->name = $rs['name'];
$this->address = $rs['address'];
}
public function getEmployees()
{
... SQL to get employees ...
while ($rs = mysql_fetch_array)
{
$this->employees[$rs['id']] = $rs['name'];
}
}
public function getOffices()
{
... SQL to get offices ...
while ($rs = mysql_fetch_array)
{
$this->offices[$rs['id']] = $rs['office'];
}
}
public function getCoffeeMachines()
{
... SQL to get coffee machines ...
while ($rs = mysql_fetch_array)
{
$this->coffeeMachines[$rs['id']] = $rs['coffeeMachine'];
}
}
}
对于什么是值得的,我怀疑后者,但可以使用其他意见。
谢谢
我认为这是一个偏好和性能问题。如果您可以阅读具有可接受启动性能的子集,则可以选择这样做。延迟初始化将在开始时更快加载,但您需要防止尝试多次填充集合。
我认为在需要时运行查询是最好的方法,这会使代码比第一种方法更具可读性和可理解性。第二种方法中的修改也很容易。
这取决于您的需求。
$company = new Company();
$company->addOffice();
$company->getOffices();
此代码将在每个路上产生不同的结果。延迟加载是很好的做法。
当我面对这样的情况时,我更愿意将物体分开。所以,我不会有一个查询选择公司类员工的数据。我会员工的实例,该实例会这样定义:
company.php
<?php
require_once "employee.php";
class company{
protected $employee;
protected $company_name;
protected $company_id;
function company($id_company,$employee=null){
if($employee){
{
$this->employee = $employee;
}
//setting company methods values. Here I have the company query
//...
}
}
function get_company_employees(){
if($this->employee){
return $this->employee->get_employees_by_company_id($this->company_id);
}else{
//throw same exception
}
}
}
employee.php
<?php
class employee{
//employee class attribs
function employee($employee_id=null){
if($employee_id){
//load data from db to class attribs. Here I have a query to get one employee
}
}
function get_employees_by_company_id($company_id){
//select employees from db and populate collection to return. Here I have a query to return employees from one company
return array();
}
}
所以,当我需要的公司员工,我会这样做:
<?php
require_once "company.php";
require_once "employee.php";
$comp_id = 1;
$company = new company($comp_id, new employee);
print_r($company->get_company_employees());
?>
哪里有好处?员工的行为比向公司检索信息要大。它本身就是一个实体。那么,为什么要将其逻辑扩展到您的代码?将逻辑放在自己的类中,如果其他类需要它,就使用它。请注意,公司有检索其员工的方法。但公司没有理由去获得它。
如果你不需要,它仍然不能加载员工。如果需要员工,请在需要时决定何时加载。
当然,我用你的例子来引导我通过我的答案。但是这种方法可以扩展到您的所有需求。