PHP
  Home arrow PHP arrow Page 2 - 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 - Defining the MVC schema's first element: constructing a basic PHP controller


    (Page 2 of 5 )

    In order to start drawing the general structure of the Model-View-Controller schema (from this point onward, I'll reference it as MVC) that I plan to build, I'll provide you with a basic hands-on example, which can be appropriately understood with only minor complications.

    In short, my example will be structured as follows: I'll build an object-based system, which will be capable of displaying simple messages in different ways, that is in lowercase, uppercase and reverse respectively. Although certainly this system won't have an immediate application in the real world, it will serve as a good introduction to how to construct a MVC relationship with PHP.

    Having defined the basic structure of my MVC-based example, I'll begin defining the first element of this schema, in this case the controller itself. This component will be directly represented by a PHP class, which will be responsible for instructing the model (more on this in a moment, thus be patient please) on what types of views, that is lowercase, uppercase and reversed, will be properly generated.

    The definition of the example looks really simple, therefore here is how the controller class, which I called "MessageController" will look:

    // define 'MessageController' class (controller)
    class MessageController{
        private $viewRanges=array('uppercased','lowercased','reversed');
        private $view;
        // constructor
        public function __construct($view='lowercased'){
            if(!in_array($view,$this->viewRanges)){
                throw new Exception('Invalid type of view!');
            }
            $this->view=$view;
        }
        // return type of view
        public function getView(){
            return $this->view;
        }
    }

    If you examine the signature of the above class, then you'll quickly realize what it does. Basically, this class accepts a type of view to be applied to the future model (as you know, in this example the model will be made up of simple message strings), and stores the inputted view as a new class property.

    Aside from the corresponding checking code, pointed out to verify the validity of the view passed in as an argument, the class exposes the "getView()" method, which comes in handy for returning precisely the type of selected view to the calling code. Nothing unexpected, right?

    As you'll probably agree, the panorama looks really simple when it comes to creating a controller class with PHP. Now, let's move on and see how a model class can be included as part of the MVC schema.

    Based on the practical example that I'm currently developing, the model should be another PHP class, which would accept as an incoming parameter the controller object that you saw before, in order to display messages either in lowercase, uppercase or reversed. Now, do you see the logic followed by the MVC relationship? Great! Therefore, to see how the model class will be created, please click on the link shown below and continue 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

    - Using Aliases and the Autoload Function with...
    - 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
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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