MVC in PHP, A Real World Example - Tom Butler
MVC in PHP, A Real World Example - Tom Butler
Home
About me
Contact
Code
Dice - PHP Dependency Injection Container
Articles
Model-View-Confusion series
1. Hello World
2. Real world example (part 1)
3. Deploying MVC on the web
4. Create a router using Dependency Injection
MVVM
Dependencies in code
Constructor Injection vs Setter Injection
The "courier" anti-pattern.
Are Static Methods/Variables bad practice?
Best Practices
Programming Philosophy
Is programming an art form?
The importance of getting terminology correct
SoCcam's Razor: Applying the Single Responsibility Principle using a practical approach
Other
A better solution to coding standards
PHP Autoloaders should not be case sensitive
PHP: PSR-0: Pretty Shortsighted, Really
Find every combination of an array
Why I don't have a comments section
Split/explode a string in PHP with an escape character
Introduction
This is a step by step tutorial for implementing MVC in PHP in a real world application. I've posted many
articles which are hypothetical and full of the theory behind MVC but today here's something for the
pragmatists. Something you can see be developed one step at a time to produced a real application: The
application will be a currency converter. It will convert from a chosen currency to several others and display the
values in each currency.
This example will show a bare-bones, lightweight MVC application and finish by highlighting some of the
problems which arise when using this approach. At the end you'll understand the proper separation of concers of
MVC and at the same time, be aware of the limitations of assuming the domain model and MVC model are the
same thing.
class CurrencyConverter {
private $baseValue = 0;
private $rates = [
'GBP' => 1.0,
];
if (isset($this->rates[$currency])) {
$rate = 1/$this->rates[$currency];
else return 0;
if (isset($this->rates[$currency])) {
As you can see, this stores the exchange rates relative to GBP (British Pounds) and performs the conversion. It
stores a specific amount always stored as the value in GBP. However, the base value is not directly accessible.
The maths involved and implementation of this class isn't important, what is important is that the class can be
used to do conversions like this:
$currencyConverter->set(100, 'GBP');
echo '100 GBP is:';
$currencyConverter->set(100, 'USD');
This converter works without any knowledge of how it will be used, the architecture it might be used in and can
work as its own standalone component.
This is the Domain Model. For this part of the tutorial, I will use the Domain Model as the Model in MVC. The
next article will discuss moving beyond this approach and the limitations of it.
The View
Now that the domain model is working successfully the next step is to create the View. Since the currency
converter will require the user to type in a value in the currency they're converting from an input box will be
required as well as a submit button for them to press. It's probably worth displaying the "From" currency as well.
The view will need to know which currency it's dealing with. In this instance, we'll start with GBP.
class CurrencyConverterView {
private $currency;
$this->currency = $currency;
return $html;
echo $view->output();
class CurrencyConverterView {
private $converter;
private $currency;
$this->converter = $converter;
$this->currency = $currency;
return $html;
echo $view->output();
This is not very informative. But we can test it using by setting a value in the model and displaying a different
currency:
$currencyConverter = new CurrencyConverter;
$currencyConverter->set('100', 'GBP');
echo $view->output();
Which is correctly displaying the EUR value of 100 GBP. On its own, this isn't very useful. However, because
MVC is all about reusability, the View can be reused with the other currencies:
$currencyConverter->set('100', 'GBP');
echo $gbpView->output();
echo $usdView->output();
echo $eurView->output();
echo $yenView->output();
class CurrencyConverterController {
private $currencyConverter;
$this->currencyConverter = $currencyConverter;
$this->currencyConverter->set($request['amount'], $request['currency']);
This uses the form field names which we already created in the view. In order to make the controller reusable
and testable, it doesn't use $_POST directly but takes an array which in this example is passed in as an
argument.
if (isset($_GET['action'])) $controller->{$_GET['action']}($_POST);
echo $gbpView->output();
echo $usdView->output();
echo $eurView->output();
echo $yenView->output();
And the conversion will take place based on which convert button you press. Try it yourself or view the entire
source code
Conclusion
This is an example of using MVC to solve a real world problem. Some of the key things to note about
implementing MVC are that, for this application:
As noted earlier, there are a few limitations with this approach because the Domain Model has been used as the
MVC model. These will be discussed in detail in the next article.
About the author
All content is by Tom Butler, a Ph.D student, Web Developer and University Lecturer based in Milton Keynes,
UK. Interests: Programming, best practices, PC Gaming, live music, gradually improving at Flying Trapeze.
Related Articles
1. MVC in PHP Tutorial: Hello World
2. The View gets itso wn data from the Model
3. Deploying MVC on the web
4. PHP: Annotations are an Abomination
5. Maximising View reusability with View Helpers
More...
Contact
About
Home