Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. An Introduction to Simulating the Model-View-Controller Schema in PHP
  2. Defining the MVC schema's first element: constructing a basic PHP controller
  3. Extending the MVC relationship: creating a basic model class
  4. Completing the MVC schema: defining the view component
  5. Assembling the respective elements: implementing the complete MVC schema
By: Alejandro Gervasio
Rating: starstarstarstarstar / 20
August 07, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: