MVC is OO pattern and you want to approach it with a procedural context.
This is plain wrong. MVC has nothing to do with object oriented coding.
MVC is a software architecture pattern which aims at separating the representation of information from the user's interaction with it.
How you achieve that is up to you. You can achieve that using whatever coding form you want, object oriented, procedural, functional, and what not.
With regard to the question at hand: The easiest way to achieve the MVC pattern when you're coding procedural PHP is to use a lot of small functions where each particular function has its own unique task. Don't let a function have many tasks. That way it is more easy to separate things. Also don't keep a lot of functions in the same file. Rather gather related functions together in smaller groups each group in its own file (what actually is done in OO with classes).
Yep, that about sums up MVC... but it doesn't have to be object oriented... you just need to follow a few golden rules:
The controller receives and processes input, generates any data and puts it in the model.
The view takes data from the model and renders it.
The controller should not format data for view - it should have no knowledge of how/why/what the view wants (eg doesn't insert HTML in a text string since the output could be JSON)
The view should not look-up any data for itself - if it's not in the model then the controller failed in it's job (throw/report an error).
Beyond that you can do things however you want. You'd basically need a set of procedures to act as controllers - parsing $_REQUEST vars (more likely as GET/POST/COOKIE) performing any data lookup building + filling the model, and then another set of procedures that at as views - taking what's in the model and rendering it for the user. The model can be as simple as an associative array.
Anything that organizes your code more along the lines of Models, Views, and Controllers. It may not be "pure" or "correct" MVC in some people's minds...but if it organizes your procedural code more meaningfully or teaches you more about MVC, then it's a good thing.
This is kinda weird. MVC is OO pattern and you want to approach it with a procedural context.
The first thing that comes to my mind is to have some kind of templating engine in order to split the PHP from the HTML code. This will be one big step towards procedural MVC :)
The next thing is to name functions of the same group (that means, functions that contains logic for related objects) with prefixes (like you group methods under a class name).
For example - look PHP procedural functions for working with mysql :
If you are implementing a new design pattern you most likely are refactoring or writing something from scratch. I highly recommend moving to OOP if you plan to use MVC. It could be implemented procedurally but will be very hackish and not a good solution.
There are several free MVC php solutions that you can pick up easily. Here are a few:
发布评论
评论(5)
这是完全错误的。 MVC 与面向对象编码无关。
MVC 是一种软件架构模式,旨在将信息的表示与用户的交互分开。
如何实现这一目标取决于您。您可以使用任何您想要的编码形式来实现这一目标,无论是面向对象的、过程的、函数式的还是其他形式。
关于手头的问题:当您编写过程性 PHP 代码时,实现 MVC 模式的最简单方法是使用许多小函数,其中每个特定函数都有自己独特的任务。不要让一个函数承担很多任务。这样就更容易分离事物。也不要在同一个文件中保留很多函数。而是将相关函数集中到较小的组中,每个组在其自己的文件中(实际上是在 OO 中使用类完成的)。
有人用一个简单的例子做到了这一点,没有 OO 的 MVC:http://www.fluffycat。 com/PHP-Design-Patterns/Non-OO-MVC/
This is plain wrong. MVC has nothing to do with object oriented coding.
MVC is a software architecture pattern which aims at separating the representation of information from the user's interaction with it.
How you achieve that is up to you. You can achieve that using whatever coding form you want, object oriented, procedural, functional, and what not.
With regard to the question at hand: The easiest way to achieve the MVC pattern when you're coding procedural PHP is to use a lot of small functions where each particular function has its own unique task. Don't let a function have many tasks. That way it is more easy to separate things. Also don't keep a lot of functions in the same file. Rather gather related functions together in smaller groups each group in its own file (what actually is done in OO with classes).
Someone does it here with a simple example, MVC without OO: http://www.fluffycat.com/PHP-Design-Patterns/Non-OO-MVC/
是的,这就是 MVC 的总结……但它不必是面向对象的……您只需要遵循一些黄金规则:
除此之外,你可以做任何你想做的事。您基本上需要一组过程来充当控制器 - 解析 $_REQUEST 变量(更可能为 GET/POST/COOKIE)执行任何数据查找构建 + 填充模型,然后另一组作为视图的过程 - 获取模型中的内容并将其呈现给用户。该模型可以像关联数组一样简单。
Yep, that about sums up MVC... but it doesn't have to be object oriented... you just need to follow a few golden rules:
Beyond that you can do things however you want. You'd basically need a set of procedures to act as controllers - parsing
$_REQUEST
vars (more likely as GET/POST/COOKIE) performing any data lookup building + filling the model, and then another set of procedures that at as views - taking what's in the model and rendering it for the user. The model can be as simple as an associative array.绝对地。使用以下任意组合:
任何能够更多地按照模型、视图和控制器来组织代码的东西。在某些人看来,它可能不是“纯粹的”或“正确的”MVC……但如果它更有意义地组织您的过程代码或教您更多有关 MVC 的知识,那么这是一件好事。
Absolutely. Use any combination of the following:
Anything that organizes your code more along the lines of Models, Views, and Controllers. It may not be "pure" or "correct" MVC in some people's minds...but if it organizes your procedural code more meaningfully or teaches you more about MVC, then it's a good thing.
这有点奇怪。
MVC 是 OO 模式,您希望通过过程上下文来处理它。
我首先想到的是拥有某种模板引擎,以便将 PHP 与 HTML 代码分开。这将是迈向过程 MVC 的一大步:)
接下来是用前缀命名同一组的函数(即包含相关对象逻辑的函数)(就像将方法分组在类名下一样)。
例如 - 查看用于 mysql 的 PHP 过程函数:
等。
并将这些函数分组到单独的文件中。这也将有助于解耦一些逻辑。
如果我还有其他想法,我会编辑我的帖子:)
This is kinda weird.
MVC is OO pattern and you want to approach it with a procedural context.
The first thing that comes to my mind is to have some kind of templating engine in order to split the PHP from the HTML code. This will be one big step towards procedural MVC :)
The next thing is to name functions of the same group (that means, functions that contains logic for related objects) with prefixes (like you group methods under a class name).
For example - look PHP procedural functions for working with mysql :
etc.
And group those functions in separate files. This will help to decouple a bit of the logic too.
If anything else come to my mind, i'll edit my post :)
如果您正在实现新的设计模式,您很可能正在重构或从头开始编写一些东西。如果您打算使用 MVC,我强烈建议您转向 OOP。它可以按程序实现,但会非常hackish并且不是一个好的解决方案。
您可以轻松获取多种免费的 MVC php 解决方案。以下是一些:
If you are implementing a new design pattern you most likely are refactoring or writing something from scratch. I highly recommend moving to OOP if you plan to use MVC. It could be implemented procedurally but will be very hackish and not a good solution.
There are several free MVC php solutions that you can pick up easily. Here are a few: