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.
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 . 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
namespace FeedReader\Model;
interface IVideoCollection {
public function __get($key);
public function __set($key, $value);
public function addVideo($video);
public function getVideos();
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
namespace FeedReader\View;
class YoutubeView implements IVideoView {
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.