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

config.php

config.php provides a centralized location for global configuration variables, such as the DSN and log file location. Also, notice that I dynamically figure out the installation location on the file system. This will make installing and migrating your code simple if you use FR_BASE_PATH in your own code.

index.php

This is the controller. I will cover this in depth in the next article.

Object.php

This is the base class for all of the foundation classes. It provides some basic features that most, if not all, classes will need. Additionally, the child class FR_Object_DB extends this object and provides a database connection.

The idea is that, by having all children extend from a central object, all of the foundation classes will share certain characteristics. You could put the database connection directly into FR_Object, but not all classes need a database connection. I will talk about FR_Object_DB later.



<?php



  require_once('Log.php');



  /**

  * FR_Object

  *

  * The base object class for most of the classes that we use in our framework.

  * Provides basic logging and set/get functionality.

  *

  * @author Joe Stump <joe@joestump.net>

  * @package Framework

  */

  abstract class FR_Object

  {

      /**

      * $log

      *

      * @var mixed $log Instance of PEAR Log 

      */

      protected $log;



      /**

      * $me

      *

      * @var mixed $me Instance of ReflectionClass

      */

      protected $me;



      /**

      * __construct

      * 

      * @author Joe Stump <joe@joestump.net>

      * @access public 

      */

      public function __construct()

      {

          $this->log = Log::factory('file',FR_LOG_FILE);

          $this->me = new ReflectionClass($this);

      }



      /**

      * setFrom

      *

      * @author Joe Stump <joe@joestump.net>

      * @access public

      * @param mixed $data Array of variables to assign to instance

      * @return void

      */

      public function setFrom($data)

      {

          if (is_array($data) && count($data)) {

              $valid = get_class_vars(get_class($this));

              foreach ($valid as $var => $val) {

                  if (isset($data[$var])) {

                      $this->$var = $data[$var];

                  }

              }

          }

      }



      /**

      * toArray

      *

      * @author Joe Stump <joe@joestump.net>

      * @access public

      * @return mixed Array of member variables keyed by variable name

      */

      public function toArray()

      {

          $defaults = $this->me->getDefaultProperties();

          $return = array();

          foreach ($defaults as $var => $val) {

              if ($this->$var instanceof FR_Object) {

                  $return[$var] = $this->$var->toArray();

              } else {

                  $return[$var] = $this->$var;

              }

          }

  

          return $return;

      }



      /**

      * __destruct

      *

      * @author Joe Stump <joe@joestump.net>

      * @access public

      * @return void

      */

      public function __destruct()

      {

          if ($this->log instanceof Log) {

              $this->log->close();

          }

      }

  }



?>

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