PHP
  Home arrow PHP arrow Page 3 - 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 - Extending the MVC relationship: creating a basic model class


    (Page 3 of 5 )

    As you learned in the article's previous section, building a controller class with PHP is indeed a process that doesn't present major difficulties. However, now that the appropriate class has been created, it's time to see how the model fits into the still incomplete MVC relationship.

    For this reason, I'm going to define a model class, which as I mentioned before, will take up the previous controller class as its unique input parameter. Speaking of that, below you can see the signature of the brand new "MessageKeeper" class. Please have a look at its source code:

    // define 'MessageKeeper' class (model)
    class MessageKeeper{
        private $messages=array();
        private $messageController;
        private $controllerView;
        // constructor
        public function __construct(MessageController
    $messageController){
            $this->messageController=$messageController;
            // get type of view from 'MessageController' class
            $this->controllerView=$this->messageController->getView
    ();
        }
        // add new message
        public function addMessage($message){
            if(!message||!is_string($message)||strlen($message)
    <8||strlen($message)>255){
                throw new Exception('Message is null or its length is
    invalid');
            }
            $this->messages["$message"]=$message;
        }
        // remove existing message
        public function removeMessage($message){
            unset($this->messages["$message"]);
        }
        // get all the messages
        public function getMessages(){
            return $this->messages;
        }
        // get controller view
        public function getView(){
            return $this->controllerView;
        }
    }

    As you can see, the class listed above represents the model inside the respective MVC schema. In this occasion, the "MessageKeeper" class takes up its corresponding controller object and uses its "getView()" method to determine what type of view should be utilized when a message is displayed to the browser.

    However, apart from some specific code that I wrote for defining this class, I want you to pay close attention to the relationship established between the controller class and its model. Even when they're independent entities, the controller has the capacity to indicate to the model what type of view should be used when a particular message is displayed.

    I guess this concept refutes the general misconception that claims that the implementation of controllers and models is a painful task. The previous example precisely reverses this belief!

    Also, I should mention the existence of some additional methods that belong to this class, like the "addMessages()", "removeMessages()" and "getMessages()" respectively, all of them aimed at handling eventual message strings inputted into the class in question, which are saved to the "$this->messages" array.

    So far, you've seen that I've created the first two components that comprise the MVC schema: the message controller and the model. Thus, the only thing that remains to do to complete the relationship, is to build the corresponding "View" element.

    That's exactly what I will show you over the course of the following section, therefore go ahead and read it. I'll be there, waiting for you.

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