Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正确使用面向对象的PHP MVC_Php_Correctness - Fatal编程技术网

正确使用面向对象的PHP MVC

正确使用面向对象的PHP MVC,php,correctness,Php,Correctness,在过去的一周左右,我一直在阅读和学习OOP和MVC,我想知道我是否走上了正确的道路 以下是我目前拥有的一个示例: index.php 解析从Apache mod_rewrite(.htaccess)传入的URL。在这个文件中,我还首先包括站点特定的设置和定义的变量,然后包括相关的控制器文件,然后是视图 <?php // Include the site specific settings require 'includes/settings.php'; // Include the HT

在过去的一周左右,我一直在阅读和学习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'; 
?>

转到控制器: 在这个文件中,我确保设置了表单$\u POST,然后调用模型(类)


现在,模型(类)代码:


我建议您使用框架。看一看。这不是最好的MVC框架,但它是我唯一一个用于php的框架。

仔细查看这些答案,抱歉,这不是MVC,也不是OOP@LawrenceCherone我同意MVC,但为什么不是OOP呢?一点也没有?谢谢你的链接@Esailija。我刚刚读完了所有这些,它们非常有用。@yousufmon,因为这里使用的代码是,
User
实例似乎没有任何封装,违反了SRP。。等等。基本上,这是不好的。作者应该从学习和阅读开始。+1我已经使用了几个框架,对我来说CI是最好的,可能会尝试symphony 2。谢谢你的框架建议,但我真的想学习MVC和OOP的概念,现在不想使用框架。codeigniter是一个MVC框架。对于MVC,它将为您提供文件结构以及模型、控制器和视图相互交互的方式。它的模型和视图都是通过类实现的。这就是你想要的一切。如果使用CodeIgniter构建一些应用程序,到最后你应该在一定程度上理解OOP和MVC。如果你使用CodeIgniter学习OOP和MVC,那么你做得不对。这是一段可怕的代码,充满了全局状态、php4工件和违反OOP原则、概念和法律的行为。即使按照PHP标准,它对MVC模式的解释也很糟糕。
<?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.
?>