Categories: PHPTechnology

Principles of MVC for PHP Developers

10 years ago
Share

MVC, short for Model View Controller is a design pattern governs the fundamental law that application logic should be separate from presentation. Several PHP developers, experienced and emerging, have been eager about integrating MVC into their development script but the information across the web is limited and those that exist use terms and glossary that would be hard to understand for a beginner. Most tutorials would start with business logics without actually defining design pattern basics.

MVC was first described as design patterns to be used with Smalltalk by Trygve Reenskaug back in 1979. His paper “Applications Programming in Smalltalk-80: How to use Model View Controller” laid the ground for all future MVC architectures. In simple words, MVC is just a simpler way of separating application logic from the display. The basic idea with MVC is to separate applications into 3 parts, referred as Model, View and Controller. The Controller has direct associations with the View and the Model and so does View over the model. However, the Model lays inferred association over the View and subsequently over Controller. Inferred associations can be distinguished as associations that might seem feasible from the user’s point of view, but not the actual software design.

How does the interaction take place?

  1. Users interact with “View” – submitting forms or clicking links
  2. Controller handles user input by transferring the information to Model
  3. Model in turn receives these information and add to the database to update itself
  4. The View therein checks the update and responds necessarily
  5. The View waits for the next interaction from user

MVC might seem to be a great solution for starters. However, separating logic from the display isn’t a new concept. With MVC, it is just a more streamlines process. Simpler layouts and code presentations make applications maintainable.

Business Logic

A much used term, Business Logic implies the calculation of logical processes in an application. For a simple calendar, the Business Logic will be to identify the day, date, month, etc.

Templates

Several MVC frameworks use templates to enforce DRY, facilitating easy code reuse without rewriting. However, a caution for new developers would be that some templates carry complicated syntax and you wouldn’t want learning a whole new language. It would be better to grab them before staring the development.

DRY

One of the most crucial implementations of MVC architecture is DRY or the “Don’t Repeat Yourself” philosophy. DRY is also used by Ruby on Rails allowing the developer to code once and reuse it often. The principles governing DRY suggest that changing a single element doesn’t necessarily interfere with unrelated elements. For someone conversant with Ruby on Rails, the DRY principle would be easier to understand.

Convention over Configuration

A design paradigm that aims to remove the quantity of decision making, Convention over Configuration is achieved by setting up adequate frameworks for each convention. It gets pretty easier doing the minor changes here and there.

MVC is a popular method of generating clean, powerful, and scalable and fast coding in the least time and effort. For developers, it would be worth experimenting with a few frameworks before sticking with the best.