PHP
  Home arrow PHP arrow Page 5 - An Introduction to Simulating the Model-View-Controller Schema in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 19
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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!



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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