我正在开发一个系统,我必须从过程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;
}
}
?>
我想知道如果我这样做的方式是正确的,我在某处不知道静态方法不建议他们很难测试,我想知道这是否也是我的情况。< / p>
否则,如果我将它们分成单个函数会更好吗?我知道当我包含静态类时会使用更多的CPU,但我正在节省内存,因为我没有创建任何实例。
我只是想寻找一种更有条理的工作方式,这就是为什么我喜欢使用OOP而不是使用过程的原因之一,但是众所周知,OOP可以占用更多的CPU /内存。谢谢,我会等你的意见!