我正在开发一个系统,我必须重新设计一次,从程序 ajax 文件到调用 OOP 类的 ajax 文件。
在我第一次开发类时,每个类都有一个方法,这些方法与 ajax 文件以程序方式执行的操作相同,但在我只想重新调用它们时使用其他可能有用的方法。
然后我遇到了一个问题:我开始使用继承,直到我意识到如果我需要两个或更多类,那将是一场彻底的灾难。我怎么修?我正在创建包含我可能需要的常用函数的静态类,然后我从 ajax 类中以静态方式使用单个方法调用它们。这是我刚刚创建的静态类的示例:
静态类:
<?php
class InstitutionFunctions
{
public static $Statement;
public static function InstitutionExists($InstitutionId = -1)
{
self :: $Statement->prepare('SELECT COUNT(Id) FROM institutions WHERE Id = ?');
self :: $Statement->bind_param('i', $InstitutionId);
self :: $Statement->bind_result($InstitutionCount);
self :: $Statement->execute();
self :: $Statement->fetch();
if($InstitutionCount > 0)
return true;
return false;
}
public static function BlockInstitutionOnly($InstitutionId = -1, $BlockValue = 1)
{
self :: $Statement->prepare('UPDATE institutions SET Blocked = ? WHERE Id = ?');
self :: $Statement->bind_param('ii', $BlockValue, $InstitutionId);
self :: $Statement->execute();
}
public static function BlockInstitutionStudents($InstitutionId = -1, $BlockValue = 1)
{
self :: $Statement->prepare('UPDATE classroom SET Blocked = ? WHERE Institution = ? AND Rank = '. STUDENT);
self :: $Statement->bind_param('ii', $BlockValue, $InstitutionId);
self :: $Statement->execute();
}
public static function Create($InstitutionName = '')
{
self :: $Statement->prepare('INSERT INTO institutions (Name) VALUES (?)');
self :: $Statement->bind_param('s', $InstitutionName);
self :: $Statement->execute();
}
public static function GetInstitutionIdByName($InstitutionName = '')
{
self :: $Statement->prepare('SELECT Id FROM institutions WHERE Name = ?');
self :: $Statement->bind_param('s', $InstitutionName);
self :: $Statement->bind_result($InstitutionId);
self :: $Statement->execute();
self :: $Statement->fetch();
return $InstitutionId;
}
public static function InstitutionHasAssignments($InstitutionId = -1)
{
self :: $Statement->prepare('SELECT COUNT(Institution) FROM assignments WHERE Institution = ?');
self :: $Statement->bind_param('i', $InstitutionId);
self :: $Statement->bind_result($AssignmentCount);
self :: $Statement->execute();
self :: $Statement->fetch();
if($AssignmentCount > 0)
return true;
return false;
}
public static function Delete($InstitutionId = -1)
{
self :: $Statement->prepare('DELETE FROM institutions WHERE Id = ?');
self :: $Statement->bind_param('i', $InstitutionId);
self :: $Statement->execute();
}
public static function GetInstitutionBlockValue($InstitutionId = -1)
{
self :: $Statement->prepare('SELECT Blocked FROM institutions WHERE Id = ?');
self :: $Statement->bind_param('i', $InstitutionId);
self :: $Statement->bind_result($BlockValue);
self :: $Statement->execute();
self :: $Statement->fetch();
return $BlockValue;
}
}
?>
然后 ajax 类这样调用它:
<?php
class BlockInstitution
{
public $User;
public $InstitutionId;
public $BlockValue;
public $Error;
public $ErrorN;
public function __construct($User, $InstitutionId = -1, $BlockValue = '1')
{
$this->User = $User;
$this->InstitutionId = $InstitutionId;
$this->BlockValue = $BlockValue;
}
public function ValidateAndBlock()
{
if(!$this->User->SessionExist)
{
$this->Error = 'El proceso se canceló porque tu sesión ha sido cerrada.';
$this->ErrorN = 1;
return false;
}
if($this->User->Session['Rank'] != ADMIN)
{
$this->Error = 'No estás autorizado para realizar esta acción.';
$this->ErrorN = 2;
return false;
}
if(!InstitutionFunctions :: InstitutionExists($this->InstitutionId))
{
$this->Error = 'Esta institución no existe.';
$this->ErrorN = 3;
return false;
}
InstitutionFunctions :: BlockInstitutionOnly($this->InstitutionId, $this->BlockValue);
InstitutionFunctions :: BlockInstitutionStudents($this->InstitutionId, $this->BlockValue);
$this->Error = '';
$this->ErrorN = -1;
return true;
}
}
?>
我想知道我这样做是否正确,我在某处读到不推荐使用静态方法,因为它们很难测试,我想知道这是否也是我的情况。
否则,如果我只是将它们分成单个函数会更好吗?我知道当我包含静态类时会使用更多的 CPU,但我正在节省内存,因为我没有创建任何实例。
我只是想寻找一种更有条理的工作方式,这就是为什么我喜欢使用 OOP 而不是使用程序的原因之一,但众所周知,OOP 可以占用更多的 CPU / 内存。谢谢,我会等待你的意见!