PHP
  Home arrow PHP arrow Page 4 - 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 - Completing the MVC schema: defining the view component


    (Page 4 of 5 )

    In order to create the last element that composes the MVC relationship, I need to construct a "View" component. This element, like the others, will also be represented by a PHP class, which considering the context of the example that I'm developing, will be called "ViewGenerator."

    With reference to the "ViewGenerator" class, below you can see its respective signature, thus have a look at it:

    // define 'ViewGenerator' class (view)
    class ViewGenerator{
        private $messageKeeper;
        public function __construct(MessageKeeper $messageKeeper){
            $this->messageKeeper=$messageKeeper;
        }
        // generates views
        public function generateView(){
            switch ($this->messageKeeper->getView()){
                case 'reversed':
                    return array_reverse($this->messageKeeper-
    >getMessages());
                case 'lowercased':
                    return array_map('strtolower',$this-
    >messageKeeper->getMessages());
                case 'uppercased':
                    return array_map('strtoupper',$this-
    >messageKeeper->getMessages());
            }
        }
    }

    In this case, the "ViewGenerator" class shown above demonstrates a simple way to implement a "View" element in the context of the MVC schema. Basically, the functionality of this class is limited to accepting an object of type "MessageKeeper" (remember this object is the model), and in accordance with the type of view specified by the controller, generates the corresponding array of messages, either in lowercase, uppercase or reversed.

    As you can see, this entire process is performed by the "generateView()" method, which bears little discussion here.

    Well, at this point I showed you how to implement a simple MVC relationship in PHP, by constructing three basic classes that represent the model, the view and the controller respectively. After all, the complete developing process wasn't as hard as you might have initially believed, thus it's time to leap forward and see how all these elements can be put to work together.

    The next few lines explain specifically how to implement this simple MVC schema, so if you want to learn how this will be achieved, please click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · 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