R
R
rgen32012-03-05 23:43:08
PHP
rgen3, 2012-03-05 23:43:08

MVC in PHP??????

Dear Khabravians!
Started studying design patterns in PHP. For a week now I can not find available information on the Internet.
Please share your experience of where to start, what to read and where to find simple information on this topic.
I found a lot of material on asp.net, but I would like to study it on the example of php.
And one more question: what analogues of the mvc model can you offer?

Answer the question

In order to leave comments, you need to log in

9 answer(s)
E
egorinsk, 2012-03-06
@egorinsk

What does MVC analogs mean? There are MVC modifications like MVVM or MVP (in fact, about the same), there is a 3-tier architecture (but you probably don't want to do it in PHP).
I mastered MVC using CakePHP as an example (although I don’t like the framework itself, it’s dumb, but it’s good as a training example). Just read the manual that comes with it for an example of how to make a blog using the MVC approach.
You don't need patterns yet. All patterns are described in some book by Martin Fowler (here is the list: martinfowler.com/eaaCatalog/, there is a translation into Russian, but it is bad), but in order to understand them, you must first have some experience in writing code and parsing someone else's code. If you don’t have it, these patterns will be something alien and incomprehensible to you (a lot of smart words, but why do you need it if you can write it in a simple way).
If you are (suddenly) interested in patterns in order to write more accurate, high-quality, professional and maintainable code, then it is better to be guided by generally recognized principles
: copy-pastes and edits, some data (for example, a list of countries) should be stored only in 1 place, and not in several. Following this principle improves code architecture and simplifies maintenance.
- KISS (keep it as simple as possible) - choose the easiest implementation method if there are several options, and if this does not threaten problems in the future.
- do not focus and do not use features from PHP4. He died.
Don't rely solely on HTML5/CSS3. Not everyone has iPads with poppies yet, and IE8, IE7 and IE6 are alive.
- write your code, as if after you it will be supported by a psycho-maniac who knows where you live. That is, do not write things that are difficult / impossible for another person to understand. Think about who will read your code. Occasionally put comments in difficult places. Do not scatter the logic of performing an action over 10 files. Don't make files larger than 500-1000 lines.
- give the correct names to classes, functions, constants and variables. Don't use transliteration ($chisloPokupatelei). If you don’t know English, arm yourself with Yandex.Dictionaries/Googletranslate. Choose and follow a coding standard (I recommend the Zend Coding Standard).
- code on functions can be easily turned into OOP code by turning functions into static methods and combining them into classes.

B
belkamax05, 2012-03-06
@belkamax05

Another option is to understand the structure of Code Igniter. MVC is very well implemented and very easy to learn. It is easy to understand even without knowing PHP

V
Vladimir Chernyshev, 2012-03-06
@VolCh

It's MVC too :)

<?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 "<html>Name of user with id = " . htmlspecialchars($id) . " is " . htmlspecialchars($name) . "</html>";
}

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

The first function is the model (abstracted and self-sufficient data), the second is the view (abstracted and self-sufficient data representation), the main script is the controller (interaction between the user, the model and the view).
Of course, you shouldn’t write like that (at least without special reasons), but you need to understand that this is also MVC, that MVC is not a property of OOP frameworks, and not even an OOP property, but a property of the application architecture. And in order for the architecture to have this property (use the MVC pattern), it is not at all necessary to scatter application components over a bunch of different objects and / or files, use other architecture and implementation patterns, it is enough that the data, their presentation and management are separated logically.

S
Sergey, 2012-03-05
Protko @Fesor

MVC is well represented in Django.
Model - business logic, everything related to processing and storing
Controller data - in django it is called templates ... in fact, this is the part of the program that prepares data for feeding the view.
View - a view devoid of business logic (ideally - just a view)
Look at Yii, Symfony 2 ... and go.

A
Anatoly, 2012-03-06
@taliban

No matter how stupid and utterly simple the article is, it illustrates the work of MVC well sitear.ru/material/mvc-php

S
SaleMind, 2012-03-06
@SaleMind

www.fluffycat.com/PHP-Design-Patterns/ PHP patterns + usage examples

A
AxisPod, 2012-03-06
@AxisPod

Well, first of all, MVC is a paradigm, not a pattern. In this case, there are no hard requirements, while patterns have hard requirements. Therefore, it was possible and difficult to find, because. people understand MVC as they want.

A
ainu, 2012-03-07
@ainu

> And one more question: what analogues of the mvc model can you offer?
HMVC

T
TheHorse, 2012-03-08
@TheHorse

In short, MVC and other alternatives are special cases of a more general paradigm that I personally call Layers.
Don't worry about MVC. Just separate different layers/parts of the application. Make independent modules. Decompose/composite.
You should not drag in MVC or something similar just because it's cool.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question