Click here to Skip to main content
13,410,685 members (34,712 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 10 Jun 2012


, 19 Jul 2012
Rate this:
Please Sign up or sign in to vote.
An overview of implementing MVC in PHP.


I am not a huge fan of PHP but I like the way PHP is turning into object oriented language. Still there are lot of things to make PHP fully OOL, there are plenty of support to develop enterprise level application using PHP. The usage of object via interface and class make PHP developer to develop reusable, modular solution. In this article I am giving the overview of implementing MVC in PHP.

What is MVC? 

MVC is design framework which decreases the coupling between the objects by separating business object (Model), user interface (View) and business logic (Controller). Keep in mind that MVC is not specific to any particular language and can be implemented in different languages. It is easy to implement MVC in the languages which supports OBJECT. 

MVC in Action 

I am using an MVC pattern to display videos from YouTube in our page. Of course, you can achieve this with on PHP file and some code. But when your application grows and needs maintenance it will be nightmare to maintain such application. But when you separate your business logic and UI, you can maintain and test them easily. You can get rid of tight coupling by use the dependency injection (DI via interface and constructor). 

Controller constructor accepts three parameters: a model, a view and a feed URL. As there is no way in PHP to say type of variable, we can pass any variable type. You can utilize comment and documentation in some extent to enforce passing only certain type of variables.

 * Initializes new instance of YoutubeVideoController
 * @param IVideo $model A Model
 * @param IVideoView $view A view
 * @return void
public function __construct($model, $view, $feedUrl) {
    $this->model = $model;
    $this->view = $view;
    $this->feedUrl = $feedUrl;

Note that I have used xpath to visit node and create Video object. 

Model Interface

In our application we have two types of business objects: IVideo and IVideoCollection. Yes, second is the collection of first Smile | :)  . Why are you using interface? Because there may be separate operations and attributes for different types of videos (For example from YouTube or from MetaCafe). Our model interface contains basic operation that every concrete class should have.

namespace FeedReader\Model;

 * interface IVideoCollection
 * Provides and interface for video collection model
 * @author: Pradip Shrestha
interface IVideoCollection  {

	 * Gets the property value if exists
	 * @param mixed $key Property name
	 * @return mixed value of Propery
	public function __get($key);
	 * Sets the property value if exists
	 * @param mixed $key Property key
	 * @param mixed $value Property value
	 * @return mixed value of Property
	public function __set($key, $value);
	 * Adds a video to the collection
	 * @param IVideo $property video
	public function addVideo($video);
	 * Gets all videos
	 * @return IVideo collection
	public function getVideos();
	 * Gets a video with given index
	 * @param integer $index an index
	 * @return IVideo a video with specified index
	public function getVideo($index);


Also note that I have used namespace in PHP. It is good to see PHP supporting namespace but sadly it is nowhere near to C#, Java (package). May be in future PHP will make them more usable.

View Class

Now let's look at a View Class. Our view class is implementing IVideoView interface. As the name suggests it displays the model. It just calls another file which contains HTML.  

namespace FeedReader\View;
 * YoutubeView class to display youtube videos
class YoutubeView implements IVideoView {
	 * Displays the data
	 * @param 
	public function Display($fileName, $model) {
		include __SITE_PATH . '/View' . '/' . $fileName;			

Other Classes

Index.php creates a new instance of Controller and calls its invoke method (Isn't it simple?). There is not any dirty code in index.php. To enable auto-loading of classes, we are using sp_auto_register function via Loader class. XmlParser is used to load feeds and needs lot of improvements. 


This is very simple example of utilizing MVC framework in PHP and may need lot of improvements. As PHP has started supporting object oriented programming, it is becoming easier in PHP to develop and maintain enterprise level application.

If you are familiar with ASP.NET MVC framework, you may aware that how different URL calls the different function of the same controller. There is a framework called Symfony, which is very similar to the .NET MVC framework and it is completely object oriented framework.

Future version of Drupal (Version 8) is also utilizing symfony framework. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

C Is Sharp
Software Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionFatal Error Pin
billwaddyjr26-Oct-15 10:54
memberbillwaddyjr26-Oct-15 10:54 
BugWhen setup on localhost Pin
jatin84613-Sep-15 12:15
memberjatin84613-Sep-15 12:15 
Questiondatabase connection Pin
Member 109349859-Jul-14 3:14
memberMember 109349859-Jul-14 3:14 
AnswerRe: database connection Pin
C Is Sharp19-Feb-15 3:57
memberC Is Sharp19-Feb-15 3:57 
GeneralMy vote of 1 Pin
jlawrence1129-Jul-12 18:29
memberjlawrence1129-Jul-12 18:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180221.1 | Last Updated 19 Jul 2012
Article Copyright 2012 by C Is Sharp
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid