我是关于PHP编码的新手,我目前在一个项目中进行了调用,在该项目中,我调用两个函数称为close并从两个按钮激活项目关闭并激活,但现在为了提高我需要的代码质量在一个公共函数中调用这两个函数。但直到现在我都没能做到。任何人都可以帮我这个
关闭功能
public function closeProject($projectID, $username){
$exc = $this->estConnection();
$query = ("UPDATE dbunited SET STATUS = 3, closedate = TIME() WHERE ID = $projectID");
$result_query = mysql_query($query, $exc);
$this->MonitorActivity($username, $projectID, 'Close project');
return $result;
}
激活功能
public function activateProject($projectID, $username){
$exc = $this->estConnection();
$query = ("CALL add_table_sp($projectID)");
$result_query = mysql_query($query, $exc);
$exc = $this->restartConnection();
if(false !== $result){
$query = ("UPDATE dbunited SET STATUS = 2, executedate = IFNULL(excecutedate, TIME()), closedate = NULL WHERE ID = $projectID");
$result = mysql_query($query, $exc);
$this->MonitorActivity($username, $projectID, 'Activate project');
}
return $result;
}
我想将常见行为抽象为第3种方法并调用它,任何人都可以帮助我
答案 0 :(得分:1)
查看可以抽象为新内容的常用代码,但这并不值得。我认真考虑推迟这种优化,直到有更多的代码遵循类似的模式,然后决定是将它移动到基类还是特定的帮助类。
下面标有的常用行:
关闭功能
public function closeProject($projectID, $username){
|> $exc = $this->estConnection();
|> $query = ("UPDATE dbunited SET STATUS = 3, closedate = TIME() WHERE ID = $projectID");
|> $result_query = mysql_query($query, $exc);
|> $this->MonitorActivity($username, $projectID, 'Close project');
return $result;
}
激活功能
public function activateProject($projectID, $username){
|> $exc = $this->estConnection();
|> $query = ("CALL add_table_sp($projectID)");
|> $result_query = mysql_query($query, $exc);
$exc = $this->restartConnection();
if(false !== $result){
$query = ("UPDATE dbunited SET STATUS = 2, executedate = IFNULL(excecutedate, TIME()), closedate = NULL WHERE ID = $projectID");
$result = mysql_query($query, $exc);
|> $this->MonitorActivity($username, $projectID, 'Activate project');
}
return $result;
}
可以合并为:
public function executeQuery($query){
$exc = $this->estConnection();
$result_query = mysql_query($query, $exc);
return $result_query;
}
public function closeProject($projectID, $username){
$query = ("UPDATE dbunited SET STATUS = 3, closedate = TIME() WHERE ID = $projectID");
$result = $this->executeQuery($query)
$this->MonitorActivity($username, $projectID, 'Close project');
return $result;
}
调用MonitorActivity
的行也可能会被移动,但是现在它不可能,因为它在一个地方而不是另一个地方的嵌套if语句中。我无法确定将if语句添加到调用该方法的所有位置是否有意义。
答案 1 :(得分:0)
由于这是(家庭作业?)作业,我不会给你完整的答案,而是我会指出你正确的方向=使用anonymous functions
function switchProjectState ($projectID, $userName) {
// now you can have the body of the activateProject function in the common function
$activateProject = function () {
// define the body of the activation function here
};
// now you can have the body of the closeProject function in the common function
$closeProject = function () {
// define the body of the close function here
};
// now you can have one function to do queries (DRY compliant)
$doQuery = function ($sql) {
// you should actually be using mysqli_ or PDO here .. but thats another subject
$exc = $this->estConnection();
return $result_query = mysql_query($sql, $exc);
};
// now you call the correct anonymous function based on project state (tracked in $_SESSION)
if ( $_SESSION['project_is_open'] ) {
$closeProject ($projectID, $userName);
}
else {
$activateProject ($projectID, $userName);
}
}
如果在激活和关闭函数中使用$ doQuery回调,则可以通过将其设置为DRY compliant而不是重复$ exec来提高代码质量; $查询;每个$ result_query步骤..代码重复是一件可怕的事情,你应该总是寻找删除代码重复的方法。如果你将在其他函数中进行查询,那么$ doQuery必须在类中成为一个普通的单独函数,因为其他函数将无法访问controlProject函数中的匿名函数。
项目状态当然,如果您不想在$ _SESSION中跟踪它,可以作为参数传递。或者您可以从数据库中读取项目的状态,然后决定是否应该激活或关闭它