PHP
  Home arrow PHP arrow Page 2 - 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 - 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
     

       

    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 1 Hosted by Hostway
    Stay green...Green IT