Home arrow PHP arrow Page 4 - An Introduction to Simulating the Model-View-Controller Schema in PHP

Completing the MVC schema: defining the view component - PHP

Would you like to learn how to simulate an MVC-based system with PHP? If so, you've come to the right place. This is the first article in a three-part series that will show you how to build this schema in PHP by constructing a few classes that represent what is needed.

TABLE OF CONTENTS:
  1. An Introduction to Simulating the Model-View-Controller Schema in PHP
  2. Defining the MVC schema's first element: constructing a basic PHP controller
  3. Extending the MVC relationship: creating a basic model class
  4. Completing the MVC schema: defining the view component
  5. Assembling the respective elements: implementing the complete MVC schema
By: Alejandro Gervasio
Rating: starstarstarstarstar / 20
August 07, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: