【发布时间】:2012-11-13 14:13:34
【问题描述】:
上周左右我一直在阅读和学习 OOP 和 MVC,所以我想知道我是否走在正确的轨道上。
这是我目前拥有的示例:
index.php 解析从 Apache mod_rewrite (.htaccess) 传入的 URL。在这个文件中,我还首先包含了我的站点特定设置和定义的变量,然后包含相关的控制器文件,然后是视图。
<?php
// Include the site specific settings
require 'includes/settings.php';
// Include the HTML page header
require LIBPATH . 'views/page_header.php';
//Code to parse the url passed in from mod_rewrite
require LIBPATH . 'controllers/' . $require_url . '.inc.php';
require LIBPATH . 'views/' . $require_url . '.php';
// Include the HTML page footer
require PUBLICPATH . 'includes/page_footer.php';
?>
继续使用控制器: 在这个文件中,我确保设置了 $_POST 表单,然后调用模型(类)。
<?php
if (isset($_POST)) {
$loginUser = new User();
$loginUser->email = $_POST['email'];
$loginUser->password = $_POST['password'];
$returnArray = (json_decode($loginUser->select(), true));
$_SESSION['userID'] = $loginUser->userID;
$_SESSION['firstName'] = $loginUser->firstName;
$_SESSION['lastName'] = $loginUser->lastName;
$_SESSION['email'] = $loginUser->email;
// Redirect code to admin area of the site
}
?>
现在模型(类)代码:
<?php
// Basically I'm interacting with the database and returning the data in a JSON encoded array. This is always where I check to make sure that values are set and correct before doing the database queries.
?>
这是使用 MVC 和 PHP OOP 的正确方法吗?
感谢您的意见。
【问题讨论】:
-
抱歉,这不是 MVC 也不是 OOP。 A simple MVC concept Tut
-
@LawrenceCherone 我同意 MVC 但为什么它不是 OOP ?一点都没有?
-
感谢@Esailija 的链接。我刚读完所有这些,它们非常有帮助。
-
@YousufMemon ,因为这里使用的代码是non-structured programming paradigm,
User实例似乎没有任何封装,违反SRP ..等。基本上,它很糟糕。作者应该先拿起PHP Object-Oriented Solutions,通读一遍。
标签: php correctness