An Introduction to Simulating the Model-View-Controller Schema in PHP - Assembling the respective elements: implementing the complete MVC schema
(Page 5 of 5 )
In order to see how each of the elements that compose the MVC relationship fit each other, below I set up a comprehensive example that demonstrates the concrete interaction between the classes that you learned in the previous sections. Take a look at the following script, which shows how to display a given input string in lowercase:
// display lowercased messages
try{
$messageController=new MessageController('lowercased');
$messageKeeper=new MessageKeeper($messageController);
$messageKeeper->addMessage('This is message 1');
$messageKeeper->addMessage('This is message 2');
$messageKeeper->addMessage('This is message 3');
$messageKeeper->addMessage('This is message 4');
$messageKeeper->addMessage('This is message 5');
$viewGenerator=new ViewGenerator($messageKeeper);
print_r($viewGenerator->generateView());
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
/*
displays the following:
Array ( [This is message 1] => this is message 1 [This is message
2] => this is message 2 [This is message 3] => this is message 3
[This is message 4] => this is message 4 [This is message 5] =>
this is message 5 )
*/
See how easy it was to generate lowercase messages? Fine, now examine the following example, which returns an array of uppercase messages:
// display uppercased messages
try{
$messageController=new MessageController('uppercased');
$messageKeeper=new MessageKeeper($messageController);
$messageKeeper->addMessage('This is message 1');
$messageKeeper->addMessage('This is message 2');
$messageKeeper->addMessage('This is message 3');
$messageKeeper->addMessage('This is message 4');
$messageKeeper->addMessage('This is message 5');
$viewGenerator=new ViewGenerator($messageKeeper);
print_r($viewGenerator->generateView());
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
/*
displays the following:
Array ( [This is message 1] => THIS IS MESSAGE 1 [This is message
2] => THIS IS MESSAGE 2 [This is message 3] => THIS IS MESSAGE 3
[This is message 4] => THIS IS MESSAGE 4 [This is message 5] =>
THIS IS MESSAGE 5 )
*/
And finally, take a look at the last example, which returns an array of messages in reverse order:
// display reversed messages
try{
$messageController=new MessageController('reversed');
$messageKeeper=new MessageKeeper($messageController);
$messageKeeper->addMessage('This is message 1');
$messageKeeper->addMessage('This is message 2');
$messageKeeper->addMessage('This is message 3');
$messageKeeper->addMessage('This is message 4');
$messageKeeper->addMessage('This is message 5');
$viewGenerator=new ViewGenerator($messageKeeper);
print_r($viewGenerator->generateView());
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
/*
displays the following:
Array ( [This is message 5] => This is message 5 [This is message
4] => This is message 4 [This is message 3] => This is message 3
[This is message 2] => This is message 2 [This is message 1] =>
This is message 1 )
*/
That's about it. I think that all the previous examples are quite useful for demonstrating how to implement a simple MVC schema with PHP. Of course, here I'm not showing you a more complex example, due to the fact that I want you to grasp easily the core concepts from the very beginning.
Wrapping up
In this first part of the series, I introduced the key points of how to implement a rather primitive MVC schema with PHP. Hopefully, after seeing the corresponding code samples that I provided here, you'll have a better understanding of how this kind of relationship can be constructed with a few PHP classes.
Over the course of the upcoming tutorial, I'll be taking the MVC schema to the next level by developing a complete web page controller system, where the different views will be determined by selecting several style sheets. You won't want to miss it!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |