MVC in PHP

Easy to habracha!

Began to study design patterns in PHP. A week can't find the information available on the Internet.

Please share experience, where to start, what to read and where to find simple information on the topic.

I found a lot of material asp.net but to study would it is the example of php.

And one more question: what are analogues of mvc model you can suggest?
October 8th 19 at 02:25
9 answers
October 8th 19 at 02:27
What does it mean analogues of MVC? There are modifications of type MVC MVVM or MVP (in fact, about the same), there is a 3-tier architecture (but you wouldn't want to do it in PHP).

I've learned MVC on the example of CakePHP (the framework I don't like it is stupid, but as a study example good). Just read the attached manual, which shows an example of how to make a blog using MVC pattern.

Patterns you don't need yet. All patterns described in a book by Martin Fowler (here is the list: martinfowler.com/eaaCatalog/there is a translation into Russian, but it is bad ), but to understand them, one must first have some experience writing code and parse someone else's code. If you do not have it, for you, these patterns will be something alien and incomprehensible (lots of smart words but why do it if can be simple to write).

If you (suddenly) interested in patterns in order to write more accurate, high-quality, professional and support code, here it is better to be guided by universally recognized principles:

— DRY (don't repeat) — must not be duplicate pieces of code longer than a few lines should not be generating the code by copy-paste and edit, some data (for example, the list of countries) must be stored only in 1 place, not several. Following this principle improves the code architecture, simplifies support.

— KISS (do it as simple as possible) — choose the easiest way of implementation, if there are several options, and if it is not face problems in the future.

— not oriented and do not use features of PHP4. He died.

— don't rely only on HTML5/CSS3. Still, not everyone has iPads with Macs, and live IE8, IE7 and IE6.

— write your code as if after you, it will support the psycho-maniac who knows where you live. That is, do not write such things that are difficult/impossible to understand another person. Think about who will read your code. Occasionally in difficult places to put comments. You can't just throw the logic to perform the steps of 10 files. Don't do files larger than 500-1000 lines.

— give the correct names for classes, functions, constants and variables. Don't use translit ($chisloPokupatelei). Don't know an English — arm Yandex.Dictionaries/Googletranslator. Choose and follow the coding standard (I recommend Zend Coding Standard).

code features can be easily turned into OOP code by transforming functions into static methods and unions of classes.
And, still in pursuit. About function naming. Don't call functions in the style of a do*, like doMakeRequest() or doConnect () is really ugly looks, especially when there is a function connect() and left to wonder how is it different from the doConnect(). - antonette.Gislas commented on October 8th 19 at 02:30
code features can be easily turned into OOP code by transforming functions into static methods and unions of classes.

And really no profit to obtain. If we are to change the functions for methods, object methods, and not classes, and the location of the function call to instantiate the object and call the method. Moreover, it sometimes allows you to Ungroup a few parameters to the constructor and the method itself. - Marshall.Lakin74 commented on October 8th 19 at 02:33
> And really no profit to obtain.

Profit three. 1) Functions are grouped into classes according to meaning. For example, instead of function directoryCopy(), directoryRemove (), etc. are getting a simple and logical Utils_Directory class with the methods copy(), remove(). 2) running the startup. 3) is global everywhere-visible and conflicting variables we have private static fields of the class. Encapsulation.

The main thing — to be able to give the correct name and correct to group methods into classes. - antonette.Gislas commented on October 8th 19 at 02:36
The startup is that. Group — namesas/prefixes. Private static fields — more profound refactoring, but still unsafe in the General case. - Marshall.Lakin74 commented on October 8th 19 at 02:39
I reread it here:

> in the function call to instantiate the object and call the method. Moreover, it sometimes allows you to Ungroup a few parameters to the constructor and the method

And what for? The logic is makeMeGood($a, $b, $C) cause $goodMaker = new GoodMaker($a); $goodMaker->setSomeOption($b); $goodMaker->finallyMakeMeGood($c);? We get a lot of garbage code in the class, get scattered through the application code create and use objects, and much more scribbling.

I understand if this $goodMaker then 20 times used but you propose to replace the "function call" in this chain. Let's abstract factory and the strategy we will add that already there. - antonette.Gislas commented on October 8th 19 at 02:42
Often feature is not used alone, such as $good = makeMeGood($a, $b, $c); makeGoodBetter($good); Replace with $good = new Good($a, $b, $c); $good->makeBetter(); sitema IMHO. - Marshall.Lakin74 commented on October 8th 19 at 02:45
October 8th 19 at 02:29
Still, as an option, to understand the structure of Code Igniter. There are very well implemented MVC and it is very easy to learn. It is easy to understand even without knowing PHP
at the same* - antonette.Gislas commented on October 8th 19 at 02:32
on the website of the Russian community is laid out quite detailed and clear documentation as to the functions of the framework and logic of the MVC code-igniter.ru/user_guide/overview/appflow.html - Marshall.Lakin74 commented on October 8th 19 at 02:35
But I'm not overpowered :( - antonette.Gislas commented on October 8th 19 at 02:38
October 8th 19 at 02:31
MVC is well represented in Django.
Model — business logic, all that concerns the processing and storage of data
Controller — in Django it is called templates... in fact this is the part of the program, which prepares the data for feeding to performance.
View — the View devoid of business logic (ideally just an idea)

Look at Yii, Symfony 2.go for it.
It is also useful to learn MVVM, Model-View-Presenter, Model-view-adapter... - antonette.Gislas commented on October 8th 19 at 02:34
You are "fail" in the bunch of new terms.

"design patterns" is a different "factory", "bridges", and "observers". It's like look at programming on the other hand, rather than MVC.

MVC shows where and how you store code, data, etc. In fact, MVC is the storage of data separately, display Templates separately and code separately. Right in separate files. All.
If you put in the code HTML tag into a variable kapets, you made the move from MVC. It is very simple. But design patterns do require the scrapping of the brain and of the principles of "thinking" about algorithms. Google on "gang of four". - Marshall.Lakin74 commented on October 8th 19 at 02:37
Thanks, I'll look it up ) - antonette.Gislas commented on October 8th 19 at 02:40
in Django terminology you are confused) Model there is a model (ORM etc), the controller is a "view" and the view's templates - Marshall.Lakin74 commented on October 8th 19 at 02:43
Oops, messed up the templates and vushki... Thank you corrected. - antonette.Gislas commented on October 8th 19 at 02:46
October 8th 19 at 02:33
This is also a MVC :)
<?php
// /user.php?<id-->

function getUserNameById($id) {
 mysql_connect() or die("Can't connect");
 mysql_select_db('myapp_db') or die("Can't select");

 $result = mysql_query("SELECT name FROM users WHERE id =" . mysql_escape_string($id) . "LIMIT 1") or die("Can't query");
 $row = mysql_fetch_row($result) or die("Can't find");
mysql_close();
 $name = $row[0];

 return $name;
}

public function showUser($id, $name) {
 echo "Name of user with id =" . htmlspecialchars($id) . " is " . htmlspecialchars($name) . "";
}

$user_id = empty($_GET['id']) ? $_GET['id'] : null;
$user_name = getUserNameById($user_id);
showUser($user_id, $user_name);

The first is to model (abstract and self-sufficient data), the second view (of an abstract and self-contained presentation of data), the main script — controller (interaction between user, model and view).

To write well, of course it is not necessary (at least not without special reasons), but understand that this is also a MVC that MVC is not a property of OOP-frameworks and even not the property of the PLO, and the property of the architecture of the application you need. And architecture possessed this property (used MVC pattern) does not need to scatter components of the application on a bunch of different objects and/or files that use other patterns in the architecture and implementation, enough data, their presentation and management were separated logically.
October 8th 19 at 02:35
Whatever stupid and missing a simple article, but it is well illustreret work MVC sitear.ru/material/mvc-php
chtivo.webhost.ru/articles/mvc.php But more difficult. - antonette.Gislas commented on October 8th 19 at 02:38
October 8th 19 at 02:37
www.fluffycat.com/PHP-Design-Patterns/ patterns in PHP examples of usage
October 8th 19 at 02:39
Well, first of all the MVC paradigm, not a pattern. In this case, no stringent requirements, while the patterns of the hard requirements. It is therefore possible and it was difficult to find as people understand MVC as you want.
MVC pattern. Proof — martinfowler.com/eaaCatalog/modelViewController.html - antonette.Gislas commented on October 8th 19 at 02:42
October 8th 19 at 02:41
>And another question: what are analogues of mvc model you can suggest?
HMVC
October 8th 19 at 02:43
In short, MVC and other alternatives are special cases of a more General paradigm, which I personally called Layers.
Don't worry about MVC. Just separate different layers/parts of the application. Make independent modules. Decomposite/compositrice.

Not necessary to drag the MVC or something similar just because it's cool.
+1000! Just prepare an article on this topic. The level of structuring should correspond to the information amount, should be justified. MVC the approach has its own niche. The lack of architecture and the nonce is bad. Although it has its reasons... - antonette.Gislas commented on October 8th 19 at 02:46

Find more questions by tags MVCPHPProgrammingComputer networks