Implementing MVC in PHP: The Model
by Joe Stump03/02/2006
This article series (starting with Understanding MVC in PHP and continued in Implementing MVC in PHP: The Controller and Implementing MVC in PHP: The View) demonstrates how to build an MVC web framework using PHP 5. This article explains the Model part of Model-View-Controller design.
The Model is where the majority of the application's logic sits. It is where you run queries against the database and perform calculations on input. A good example of what a Model would look like is a simple login script. The login script gets user input from a form, validates it against the database, and then logs in the user.
The first application using the newly created framework will be the users module. I will be creating three Models:
- login.php validates input from a form against the database
- logout.php logs a user out and destroys the associated session
- whoami.php displays simple user information, similar to the Unix program of the same name
Because I am introducing the idea of sessions and users into the framework, I will need to create a few more foundation classes as well as a table in a database. Before I go over the code of login.php, I'd like to walk through these classes.
FR_Session
FR_Session is a wrapper for the built-in PHP sessions. The code isn't very involved and provides only basic support for starting, destroying, and writing sessions.
<?php
/**
* FR_Session
*
* @author Joe Stump <joe@joestump.net>
* @copyright Joe Stump <joe@joestump.net>
* @license http://www.opensource.org/licenses/gpl-license.php
* @package Framework
* @filesource
*/
/**
* FR_Session
*
* Our base session class as a singleton. Handles creating the session,
* writing to the session variable (via overloading) and destroying the
* session.
*
* @author Joe Stump <joe@joestump.net>
* @package Framework
*/
class FR_Session
{
/**
* $instance
*
* Instance variable used for singleton pattern. Stores a single instance
* of FR_Session.
*
* @author Joe Stump <joe@joestump.net>
* @var mixed $instance
*/
private static $instance;
/**
* $sessionID
*
* The session ID assigned by PHP (usually a 32 character alpha-numeric
* string).
*
* @author Joe Stump <joe@joestump.net>
* @var string $sessionID
*/
public static $sessionID;
// {{{ __construct()
/**
* __construct
*
* Starts the session and sets the sessionID for the class.
*
* @author Joe Stump <joe@joestump.net>
*/
private function __construct()
{
session_start();
self::$sessionID = session_id();
}
// }}}
// {{{ singleton()
/**
* singleton
*
* Implementation of the singleton pattern. Returns a sincle instance
* of the session class.
*
* @author Joe Stump <joe@joestump.net>
* @return mixed Instance of session
*/
public static function singleton()
{
if (!isset(self::$instance)) {
$className = __CLASS__;
self::$instance = new $className;
}
return self::$instance;
}
// }}}
// {{{ destroy()
public function destroy()
{
foreach ($_SESSION as $var => $val) {
$_SESSION[$var] = null;
}
session_destroy();
}
// }}}
// {{{ __clone()
/**
* __clone
*
* Disable PHP5's cloning method for session so people can't make copies
* of the session instance.
*
* @author Joe Stump <joe@joestump.net>
*/
public function __clone()
{
trigger_error('Clone is not allowed for '.__CLASS__,E_USER_ERROR);
}
// }}}
// {{{ __get($var)
/**
* __get($var)
*
* Returns the requested session variable.
*
* @author Joe Stump <joe@joestump.net>
* @return mixed
* @see FR_Session::__get()
*/
public function __get($var)
{
return $_SESSION[$var];
}
// }}}
// {{{ __set($var,$val)
/**
* __set
*
* Using PHP5's overloading for setting and getting variables we can
* use $session->var = $val and have it stored in the $_SESSION
* variable. To set an email address, for instance you would do the
* following:
*
* <code>
* $session->email = 'user@example.com';
* </code>
*
* This doesn't actually store 'user@example.com' into $session->email,
* rather it is stored in $_SESSION['email'].
*
* @author Joe Stump <joe@joestump.net>
* @param string $var
* @param mixed $val
* @see FR_Session::__get()
* @link http://us3.php.net/manual/en/language.oop5.overloading.php
*/
public function __set($var,$val)
{
return ($_SESSION[$var] = $val);
}
// }}}
// {{{ __destruct()
/**
* __destruct()
*
* Writes the current session.
*
* @author Joe Stump <joe@joestump.net>
*/
public function __destruct()
{
session_write_close();
}
// }}}
}
?>