-1

One interviewer asked me an interesting question "what are things you should already know if I ask you to create your own MVC framework". Apart from taking a front controller as a base and redirecting our requests accordingly, I could hardly give a specific answer to the point. Would be glad if someone can shed some light.

tereško
  • 58,060
  • 25
  • 98
  • 150
Siri
  • 1,344
  • 2
  • 10
  • 10
  • 1
    I think at the heart of it all is a router (as you described), and then understanding filesystems, dependencies, and OOP to get controllers to work. Ultimately it depends on how complex or tightly-coupled your application can be when determining how well-built it should be. I would have probably asked for a general example on what type of website it would be: ecommerce? personal? .etc. To determine how I would continue answering the question. Generally speaking, codeigniter is only good for very simple website as its dependency injection and general design doesn't lend itself to expand-ability – Alex Jun 08 '19 at 17:40

3 Answers3

4

Your answer should be:

  • An application based on MVC respects the separation of concerns principle. First separation: the UI logic from the business logic (the "M" component). The second separation (pertaining the UI logic): the user request dispatching logic (the "C" component) from the presentation logic (the "V" component).
  • The "M" component is unaware of any other application components and is implemented in such a way, that it can be shared by multiple applications (even of different types).
  • Each component can be modeled and implemented in different ways. Here can be discussed, depending on the requirements, which objects should be used and how they should interact with each other... In other words, this is the part where you would "hardly give a specific answer to the point". Below is an example presenting my chosen approach on the workflow of a web application using MVC.
  • The advantages of (developing an application using) the MVC pattern: components reusability, good testability, the possibility to easily perform changes on a certain component based on developer's specialization. Other advantages could be discovered by viewing/reading the first resources posted at the end of this answer.
  • As for the disadvantages: added complexity (I, personally, don't see any other).
  • Object-oriented programming, SOLID principles, clean architecture, clean code, design patterns, unit testing, etc.

Example of a web application implementing MVC:

Here is an overview of my chosen approach on the workflow of a web application using MVC - mainly inspired by Robert Martin's presentation Keynote: Architecture the Lost Years and trying to respect the workflow of the original MVC pattern presented by Trygve Reenskaug in 1979 (e.g. the controller updates the model, the view pulls data from it, irrespective of the controller).

You can read more details about each component in this older answer of mine.

enter image description here

enter image description here

enter image description here

Some resources:

PajuranCodes
  • 303
  • 3
  • 12
  • 43
0

If I ever have faced this question, I would suggest the questioner to go one step back and think about the problem that he wants to solve.

I would have replied with the question: How you end up believing that building an MVC framework from scratch will provide solution to your problem?

MVC pattern comes with some great advantages but also very big disadvantages. During the process of PHP evolution, some years ago we introduced ourselves to the era of MVC frameworks and it was great! Faster and easier development process, a lot of magic, active record etc.

As PHP community evolves, we are now learning the hard way about our mistakes in the past. Extreme high coupling, edge case inheritance, use of globals and implementations that are breaking every SOLID rule. MVC is abused by frameworks and the way that is implemented by them is fundamentally wrong! Concept of middleware will be the only inheritance that will remain after the fall of MVC frameworks.

So, apart from the code reusability issue that can be raised, in order to build (or better start/suggest) a (MVC) framework, in my opinion technical knowledge is the least that you will need. You need to follow PHP's and PHP Framework evolution path and learn from past mistakes in order to make your choices.

  • 2
    It was probably less of an "I've got x problem that is outside the scope of any MVCs currently on the market and thus I must build my own MVC" and more of a "Do you actually understand *how* MVCs work? Demonstrate this by telling me some of the core approaches (include advantages/disadvantages if you want for each approach)"... I forget who said it, but true mastery is being able to understand something enough to explain it simply and I think that was the goal here. – Alex Jun 08 '19 at 17:36
  • Thanks for your comment @Alex! Actually after reading the question again and not in rush, I think you are right. I am thinking of leaving my answer as it is though, since IMO it is good for someone that has the first question as you explain it to have insights about how I see it – Chrysovalantis Koutsoumpos Jun 08 '19 at 19:27
  • If I may, Chrysovalantis, I disagree with your statement _"MVC is fundamentally wrong"_. Separating the concerns is a right thing to do. So, using the MVC pattern is fundamentally right. Though, various applications based on MVC (be they web frameworks as well) can be wrongly implemented. And, in my opinion, the very big disadvantages you are speaking about are the effect of such implementations, not of the MVC pattern. As for middlewares, as I see it, they are either just an alternative to the "C" component of MVC, or a complement to it. The "V" and the "M" components of MVC remain unchanged. – PajuranCodes Jun 09 '19 at 01:08
  • oh yea also disagree about that part too. mvc is the solution not the problem. but codeigniter is a good example of the "wrong" type of mvc. however, imo, it is good for small-scale applications. – Alex Jun 09 '19 at 01:20
  • Good catch! I more mean ‘MVC with the way that is implemented by frameworks’ exactly as you describing it @dakis. I will amend that part to be specific since it creates confusion. Of course, there is nothing so bad with MVC as a pattern. I also agree that despite my opinion are good choices for small scale projects were performance and micro optimization is not required Alex. I use them my self as well. In general I think that we are moving to the era of functional/procedural way of doing things. The era of middlewares, carried functions etc. Pure, reusable & low coupling implementations. – Chrysovalantis Koutsoumpos Jun 09 '19 at 07:32
  • To give you more context about my opinion (which may look absolute), I recently attended a presentation (https://youtu.be/v1I57-_Rsv0) and discussed with Marco regarding those issues and the evolution of the language and frameworks. It seems that the internals team and a big part of the php community is moving away of the current way of doing things. Some evidence of this is that the most popular frameworks and products left PHP FIG which enforced commonalities and coupling in a big way [...] – Chrysovalantis Koutsoumpos Jun 09 '19 at 08:05
  • Hi, Chrysovalantis. The [PSRs](https://www.php-fig.org/psr/) are interfaces. By using them in your project you have the big chance to inject different implementations on-demand. So, ideally, if you'd want to use Zend Diactoros instead of your own PSR-17 implementation, you **should** be able to do it without changing one line of code. Though, because Zend **wrongly** puts at disposal "proprietary" functions like [ServerRequestFactory::fromGlobals](https://github.com/zendframework/zend-diactoros/blob/master/src/ServerRequestFactory.php#L52) in its factory class, you **can't** do it. – PajuranCodes Jun 09 '19 at 21:55
  • You are forced to change your project code, in order to make use of Diactoros' functionality. So, the PHP FIG doesn't enforce coupling in any way. The wrong implementations of the interfaces provided by the PHP FIG team are doing it... Otherwise, I watched the presentation. An interesting one. – PajuranCodes Jun 09 '19 at 22:02
  • P.S: I rectify: _"... you should be able to do it without changing one line of code, except of the needed changes in the DI container"_. – PajuranCodes Jun 09 '19 at 22:12
  • I get what you are saying regarding PSRs and I couldn't agree more. What I wanted to point out, is that most of PHP-FIG personnel are the project representatives or members core teams of those projects that were implementing PSR's the wrong way. – Chrysovalantis Koutsoumpos Jun 10 '19 at 16:18
  • Oh, I see. Thanks for the clarifications. – PajuranCodes Jun 10 '19 at 18:43
  • @ChrysovalantisKoutsoumpos you might find this interesting related to the video you posted: https://blog.ircmaxell.com/2016/05/all-about-middleware.html – Alex Jun 11 '19 at 23:37
  • Thanks @Alex. An interesting article indeed. I think they wanted to do something similar for the event dispatcher and Symfony took off – Chrysovalantis Koutsoumpos Jun 12 '19 at 07:06
0

I guess the simplest answer to this question would be exactly explaining what is a a php mvc framework, what it does and what are the benefits like talking about all the classes you use occasionally use in the framework that you can't work without it:

  • Config
  • URI
  • Input
  • Output
  • Language
  • Encryption
  • Etc.

And describe briefly the usage of each one of them, and talk about database classes:

  • Database Configuration
  • Connections
  • Query Builder
  • Etc.

Maybe talk about loaders, libraries, helpers and so on

Sherif Salah
  • 2,085
  • 2
  • 9
  • 21