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

Implementing MVC in PHP: The Model

FR_User

FR_User is a basic data object in the users table. I created a single table called fr_users in a MySQL database called framework:



CREATE TABLE `fr_users` (

  `userID`    int(11) unsigned NOT NULL auto_increment,

  `email`    char(45) NOT NULL         default '',

  `password` char(15) NOT NULL         default '',

  `fname`    char(30) NOT NULL         default '',

  `lname`    char(30) NOT NULL         default '',

  `posted`   datetime NOT NULL         default '0000-00-00 00:00:00',

  `status`   enum('active','disabled') default 'active',

  PRIMARY KEY  (`userID`),

  UNIQUE KEY `email` (`email`),

  KEY `posted` (`posted`),

  KEY `status` (`status`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;



-- Special user for anonymous/not-logged-in users

INSERT INTO `fr_users` VALUES (0,'anon@example.com','','Anonymous','Coward', \

    '2005-06-05 11:33:56','disabled');
<?php



  /**

  * FR_User

  *

  * @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_User

  *

  * Base user object.

  *

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

  * @package Framework

  */

  class FR_User extends FR_Object_DB

  {

      public $userID;

      public $email;

      public $password;

      public $fname;

      public $lname;

      public $posted;



      public function __construct($userID=null)

      {

          parent::__construct();



          if ($userID === null) {

              $session = FR_Session::singleton(); 

              if (!is_numeric($session->userID)) {

                  $userID = 0; 

              } else {

                  $userID = $session->userID;

              }

          }



          $sql = "SELECT *

                  FROM fr_users

                  WHERE userID=".$userID." AND 

                        status='active'";



          $result = $this->db->getRow($sql);

          if (!PEAR::isError($result) && is_array($result)) {

              $this->setFrom($result);

          }

      }



      public function __destruct()

      {

          parent::__destruct();

      }

  }



?>

Notice that the constructor looks around for user IDs in various forms. If it receives a user ID, the class fetches the given user ID. If it does not receive a user ID, it checks the session; if a user ID is not present, it assumes the user is not logged in and it loads the special anonymous user record.

The special anonymous user allows me to have a special user for people not logged in. It allows me to easily and quickly check whether a user is logged in:

...



  if ($this->user->userID > 0) {

      // User is logged in

  }



...

I would have much rather made FR_User a singleton; however, PHP5 does not allow a child class to have a private constructor when the parent class, FR_Object_DB, has a public constructor.

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

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

  • Next Article-PHP:
  • Related Materias
    Important Notice for Apach
    Custom-Compiling Apache an
    Simplify Your Life with Ap
    Industrial-Strength Webcas
    Monitoring Apache Page-Loa
    Implementing MVC in PHP: T
    Simplify PHP Development w
    Improve Your Build Process
    Implementing MVC in PHP: T
    Important Notice for PHP D
    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