PHP
  Home arrow PHP arrow Page 5 - An Introduction to Simulating the Mode...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

An Introduction to Simulating the Model-View-Controller Schema in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    2006-08-07

    Table of Contents:
  • An Introduction to Simulating the Model-View-Controller Schema in PHP
  • Defining the MVC schema's first element: constructing a basic PHP controller
  • Extending the MVC relationship: creating a basic model class
  • Completing the MVC schema: defining the view component
  • Assembling the respective elements: implementing the complete MVC schema

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · The first article of this series shows in a friendly fashion how to simulate the...
       · Hi,Personally, I think it's strange to have the model know anything about the...
       · This article make a serious error in having the Model dependent on the Controller....
       · Hi,Thank you for commenting on this PHP article. Concerning your opinion, as I...
       · Thank you for your comments on this tutorial. As I posted above, this was an...
       · perhaps a tutorial on a full system broken into several articles might be a good...
       · Hello Jon,Thank you for posting your comments on my PHP article. I appreciate...
       · After all of this constructive feedback concerning the controller it might be a good...
       · Hi Jon again,I introduced a correction into the article, as you...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT