PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to PHP Subscribe to Newsletters

Implementing MVC in PHP: The Model
Pages: 1, 2, 3, 4

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.

Pages: 1, 2, 3, 4

Next Pagearrow




Recommended for You

  1. Cover of Mastering Regular Expressions
    Mastering Regular Expressions
    Print: $44.99
    Ebook: $35.99
  2. Cover of PHP Anthology
    PHP Anthology
    Print: $39.95
  3. Cover of PHP Security Collection
    PHP Security Collection
    Ebook: $5.95
  4. Cover of Learning PHP, MySQL, and JavaScript
    Learning PHP, MySQL, and JavaScript
    Print: $39.99
    Ebook: $31.99

Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com