Click here to Skip to main content
15,882,114 members
Articles / Programming Languages / PHP
Tip/Trick

MVC in PHP

Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
19 Jul 2012CPOL3 min read 95.9K   4.4K   12   6
An overview of implementing MVC in PHP.

Introduction

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.

PHP
/**
 * 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.

PHP
<?php
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.  

PHP
<?php
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. 

Conclusion

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. 

License

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


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFatal Error Pin
billwaddyjr26-Oct-15 9:54
billwaddyjr26-Oct-15 9:54 
BugWhen setup on localhost Pin
jatin84613-Sep-15 11:15
jatin84613-Sep-15 11:15 
When setup on localhost
Please check:

Fatal error: Interface 'FeedReader\View\IVideoView' not found in D:\xampp\htdocs\FeedReader\View\YoutubeView.php on line 7
Questiondatabase connection Pin
Member 109349859-Jul-14 2:14
Member 109349859-Jul-14 2:14 
AnswerRe: database connection Pin
C Is Sharp19-Feb-15 2:57
C Is Sharp19-Feb-15 2:57 
AnswerRe: database connection Pin
ahmadreza taheri11-Oct-20 19:19
ahmadreza taheri11-Oct-20 19:19 
GeneralMy vote of 1 Pin
jlawrence1129-Jul-12 17:29
jlawrence1129-Jul-12 17: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.