Home arrow PHP arrow Page 3 - Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP

Setting up the basics for generating disparate views: defining a controller based on MySQL datasets - PHP

Are you a curious PHP developer, wanting to learn how to simulate a basic Model-View-Controller schema with PHP? Then, look no further, because you’re in the right place. Welcome to the final part of the series “Simulating the Model-View-Controller Schema in PHP.” In three tutorials, this series provides you with a comprehensive introduction, aimed at implementing a MVC-based relationship between PHP objects.

TABLE OF CONTENTS:
  1. Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP
  2. The starting point of a brand new MVC schema: defining some MySQL processing classes
  3. Setting up the basics for generating disparate views: defining a controller based on MySQL datasets
  4. Defining the next link of the chain: creating a model founded on native MySQL data sets
  5. Generating distinct views from a single MySQL result set: creating an output generator class
  6. Putting the classes to work together: seeing the MVC schema in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 14
August 21, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Since I plan to implement an MVC-based schema by using native MySQL result sets, obviously one of the first things I have to do is create a result set controller, which can be directly represented by a PHP class. The first link of this programming chain is the class below, which I called "ResultController," and its definition is as follows:

// define 'ResultController' (controller)
class ResultController{
    private $outputRanges=array('xhtml','xml','plain');
    private $output;
    public function __construct($output='xhtml'){
        if(!in_array($output,$this->outputRanges)){
            throw new Exception('Invalid result set output!');
        }
        $this->output=$output;
    }
    public function getOutput(){
        return $this->output;
    }
} 

As you can see, this new "ResultController" class is responsible for instructing the corresponding model (keep in mind again that the model is represented by a MySQL result set) about what type of output should be generated. In this case, the class in question will handle three specific kind of views for rendering: PLAIN, XHTML, and XML respectively, thus any other type of output will be refused by this class.

Like the examples shown in the previous two articles of this series, the above class also exposes an accessor method (the "getOutput()" method), which comes in very handy for retrieving the respective output selected by the controller. Still with me? Good, now that you know how this entirely new controller class looks, let me show you the next class that composes the MVC schema.

Yes, you're correct! In the next few lines I'll define the model class, which not surprisingly will be represented by a MySQL dataset wrapper. Therefore, keep reading to learn how this class will be coded.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: