【发布时间】:2014-02-25 09:37:16
【问题描述】:
我刚开始为我的项目使用 PHP 中的 OOPS。并希望确保我为项目学习正确的 OOP 方法。
我的班级是这样的:
class user
{
//class variables
public function __construct
{
//initializes all class variables to null
}
public function setById($id)
{
//Queries Database for row matching $id and sets the class variables with
the same values as returned in result
}
public function setValues($array)
{
foreach($array as $key => $value)
{
$this->$key = $value;
}
}
public function update()
{
//updates the database table from the class variables
}
public function delete()
{
//deletes the row matching the class variable called id
}
}
现在进行更新操作:
1) 我将首先声明对象。 2) 然后调用 SetById() 函数,并将参数作为我希望更新的行的 id。这将设置数据库中的所有当前值。 3) 然后将调用 setValues() 函数来更新类变量中的值。 4) 然后调用update函数根据类变量的值更新数据库行。
这是更新 OOP 方法的正确方法吗?
对于删除操作:
1) 我将首先声明对象。 2) 然后调用 SetById() 函数,并将参数作为我希望更新的行的 id。这将设置数据库中的所有当前值。 3) 然后调用delete函数删除类变量中匹配id的行。
这是正确的做法吗?
【问题讨论】: