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

Extending the MVC relationship: creating a basic model class - 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.

  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



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
        // get type of view from 'MessageController' class
    // add new message
    public function addMessage($message){
            throw new Exception('Message is null or its length is
    // remove existing message
    public function removeMessage($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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: