HomePHP 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.
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.