Now: Tutorial for Web and Software Design > PHP > PHP Basic > PHP Content
> Understanding MVC in PHP [Bookmark it]
Understanding MVC in PHP

Understanding MVC in PHP

The New Way

By bringing everything into an MVC framework, I could make my life a lot easier. Compare the following code:

<?php



class myapp extends FR_Auth_User

{

    public function __construct()

    {

        parent::__construct();

    }



    public function __default()

    {

        // Do something here

    }



    public function delete()

    {



    }



    public function __destruct()

    {

        parent::__destruct();

    }

}



?>

Notice that this code has no apparent concern with connecting to a database, verifying the user is logged in, or outputting anything. The controller handles all of this.

If I wanted to authenticate against LDAP, I could create FR_Auth_LDAP. The controller could recognize certain output methods (such as $_GET['output']) and switch to the PDF or SOAP presenter on the fly. The event handler, delete, handles only deleting and nothing else. Because the module has an instance of the FR_User class, it's easy to check which groups that user is in, etc. Smarty, the template engine, handles caching, of course, but the controller could also handle some caching.

Switching from the old way to the MVC way of doing things can be a completely foreign concept to some people, but once you have switched, it's hard to go back. I know I won't be leaving the comforts of my MVC framework anytime soon.

Creating the Foundation

I'm a huge fan of PEAR and the PEAR_Error class. PHP 5 introduced a new class, Exception, which is almost a drop-in replacement for PEAR_Error. However, PEAR_Error has a few extra features that make it a more robust solution than Exception. As a result, the framework and foundation classes will use the PEAR_Error class for error handling. I will use Exception, however, to throw errors from the constructors, as they cannot return errors.

The design goals of the foundation classes are:

  • Leverage PEAR to quickly add features to the foundation classes.
  • Create small, reusable abstract foundation classes that will enable developers to build applications quickly within the framework.
  • Document all foundation classes using phpDocumentor tags.
  • Prepend all classes and global variables with FR to avoid possible variable/class/function collisions.

The class hierarchy will look something like this:

  • FR_Object will provide the basic features that all objects need (including logging, generic setFrom(), toArray()).
    • FR_Object_DB is a thin layer to provide child classes a database connection, along with other functions such classes might need.
      • FR_Object_Web is a thin layer that provides session and user information for web-based applications.
        • FR_Module is the base class for all applications (AKA "modules," "model," etc.) built in the framework.
          • FR_Auth is the base authentication class, which will allow for multiple authentication mechanisms.
            • FR_Auth_User is an authentication class to use in modules that require a valid, logged-in user.
            • FR_Auth_No is a dummy authentication class used for modules that require no authentication.
    • FR_Presenter is the base presentation class (the view) that will handle loading and displaying the applications after they have run.
      • FR_Presenter_smarty: the presentation layer will include the ability to load different drivers. Smarty is a great template class that has built in caching and an active community.
      • FR_Presenter_debug is a debugging presentation layer that developers can use to debug applications.
      • FR_Presenter_rest is a REST presentation layer that developers can use to output applications in XML.

Looking at the foundation classes, you can start to see the separate parts of the MVC framework. FR_Module provides for the basic needs of all of the modules (Model), FR_Presenter provides a way to display applications arbitrarily in different formats (Views). In the next part of this series I will create the controller, which will bring all of our foundation classes together in a single place.

Prev  [1] [2] [3] [4] [5] [6] Next

[Bookmark][Print] [Close][To Top]
  • Prev Article-PHP:

  • Next Article-PHP:
  • Related Materias
    Important Notice for Apach
    Custom-Compiling Apache an
    Apaches eXtended Server Si
    Python and Apache
    Newbies Find Help in OReil
    Apache::CodeRed
    Industrial-Strength Webcas
    AxKit: An XML-Delivery Too
    Module Tour: Embedding Pyt
    Apache Under Windows
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial