HomePHP Page 5 - An Introduction to Simulating the Model-View-Controller Schema in PHP
Assembling the respective elements: implementing the complete MVC schema - PHP
Would you like to learn how to simulate an MVC-based system with PHP? If so, you've come to the right place. This is the first article in a three-part series that will show you how to build this schema in PHP by constructing a few classes that represent what is needed.
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!