Understanding MVC in PHP
Pages: 1, 2, 3, 4, 5, 6

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();
          }
      }
  }

?>

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow