我有一个项目是使用结构化编程创建的,我想以最“最佳实践”方式重构为面向对象的项目。我可能是唯一一个使用这个项目的人,但这不是其他人的意思。但我可能会向其他人展示它作为卓越的例子;)PHP项目结构
我的问题很普遍,但我希望这是好的。
我在考虑让它分成三种方式;后端(主类),前端(获取和发布检查,调用类功能),可视化(使用Twig模板)。 我的项目将使用IPS论坛软件的外部整合(用户会话将保留在那里)。 有关我的代码思路,请参阅以下内容来了解如何构建此代码。
我的问题:
- 是我的总体结构确定从“前端”像这样分开的课吗?
- 我的想法是在类的外部使用IPS进行成员查找/处理,因为我稍后可以切换到前端的其他成员功能而不会影响后端?只要将成员对象放在任何地方的类中,确保至少设置了什么类的使用。
- 我应该将成员数据作为参数发送给类(构造),还是像现在一样保留它,并从前端设置公共类var?
- 我的类应该抛出异常或返回true/false并设置错误消息?
- 前端也应该是班级吗?扩展主类?
- 像这样在__construct中设置错误信息是好的,还是应该在其他地方完成?
- MyProject类应该分成多个类吗?目前在结构代码中的项目是10000行,因为我拿出大量的可视化渲染的东西,所以新的类可能大约一半。也许类为MyProjectDisplayData和MyProjectCreateData等?
- 如果对7的回答是肯定的,我是否应该为其他特定类“扩展”的消息,数据库和一般功能提供一个核心类?
- 还有别的人可能想要做不同吗?
myproject_class.php:
namespace MySpace;
use \PDO;
use \PDOException;
use \Exception;
class MyProject {
public $projectdata;
public $errormessages;
public $ips_member;
function __construct() {
//set up vars for error messages
$this->errormessages["database_queryfailed"] = "Query failed";
$this->errormessages["general_missingdata"] = "Missing data";
$this->errormessages["handling_something"] = "Some error";
}
public function displaySomeData ($id) {
if ($id == ""){
throw new Exception($this->$errormessages["general_missingdata"]);
}
try{
$sql = "GET SOME DATA FROM DB";
//PDO execute
}catch (PDOException $e) {
throw new Exception($this->$errormessages["database_queryfailed"] . " SQL: " . $sql);
}
$this->projectdata = array();
$this->projectdata["one"] = "cool";
$this->projectdata["two"] = "verycool";
if ($someerror){
throw new Exception($this->$errormessages["handling_something"]);
}
}
public function createSomeData(){
try{
$sql = "INSERT SOME DATA IN DB";
//PDO execute
}catch (PDOException $e) {
throw new Exception($this->$errormessages["database_queryfailed"] . " SQL: " . $sql);
}
}
}
前端的index.php:
require_once 'vendor/autoload.php';
require_once 'myproject_class.php';
require 'forum/init.php';
//forum initialize
\IPS\Session\Front::i();
$ips_member = \IPS\Member::loggedIn();
//load class
try {
$myproj = new MySpace\MyProject();
$myproj->ips_member = $ips_member;
} catch (Exception $e) {
die($e->getMessage()); //not die, but handle in some way
}
//check get or post var to decide what to do
if ($_GET["dowhat"] == "display"){
try {
$myproj->displaySomeData($_GET["id"]);
} catch (Exception $e) {
die($e->getMessage()); //not die, but handle in some way
}
}
//twig rendering
$loader = new Twig_Loader_Filesystem('template');
$twig = new Twig_Environment($loader);
$template = $twig->load('myproject.html');
echo $template->render(array('projectdata' => $myproj->projectdata, 'member' => $ips_member));
谢谢您的帮助!
这个问题更好的属于https://codereview.stackexchange.com/ – MikeT