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

       

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