Skip to content

Instantly share code, notes, and snippets.

@floydian
Created August 21, 2010 17:42
Show Gist options
  • Save floydian/542616 to your computer and use it in GitHub Desktop.
Save floydian/542616 to your computer and use it in GitHub Desktop.
<?php
/**
* This singleton object is responsible for keeping references to variables like the database object,
* the user object, and any other variable that is required for most pages.
*
*/
class application {
/**
* Hold an instance of the class
*/
private static $instance;
/**
* The database object.
*
* @var database
*/
private $db;
/**
* The user object. (i.e., the user accessing the page)
*
* @var database
*/
private $user;
/**
* A private constructor; prevents direct creation of object
*/
private function __construct() {
// Anything that should be done every time the application starts up can go here.
// We'll load the db connection
$this->loadDatabase();
}
/**
* The singleton method
*/
public static function summon() {
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
/**
* Prevent users from cloning the instance
*/
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
/**
* Provide a way to get access to certain private and protected properties.
*
* @param string $key
* @return mixed
*/
public function __get($key) {
switch ($key) {
case 'db': return $this->db;
case 'user': return $this->user;
default: return null;
}
}
/**
* Load the database object. This method could be called in the constructor
* so that the db is always present. Or, it can be called manually. This method
* is designed such that the db will only be loaded once. This method is chainable.
*
* @return application
*/
public function loadDatabase() {
if (empty($this->db)) {
$this->db = new database('localhost', 'user', 'pass', 'schema');
}
return $this;
}
}
<?php
abstract class base {
/**
* The application object.
*
* @var application
*/
protected $app;
/**
* The database object.
*
* @var database
*/
protected $db;
public function __construct() {
// Get a reference to the application object.
$this->app = application::summon();
// Make life easier and create a shortcut to the db object (since it'll
// probably be used a lot)
$this->db = $this->app->db;
}
}
<?php
class database {
public function __construct($host, $user, $pass, $schema) {
/*
Initialize the database connection here, whichever way you prefer to do it
*/
}
/**
* Performs a query on a database. A databaseResult object is returned.
*
* @param string $string
* @return databaseResult
*/
public function query($string) {
/*
Perform a query here, whichever way you prefer to do it
*/
// Let's assume that $resource is a result resource fetched from a database
$resource = null;
$resource = new databaseResult(databaseResult);
return $resource;
}
}
<?php
class databaseResult {
private $resource;
public function __construct($resource) {
$this->resource = $resource;
}
/**
* Returns a row from a result resource in the form of an object.
*
* @param resource $resource
* @return object
*/
public function fetchObject($resource) {
/*
Fetch an object from a database result here, whichever way you prefer to do it
*/
// I'm just setting up an empty object that will pretend to have been fetched from a result resource.
$foo = new stdClass();
return $foo;
}
}
<?php
class user extends base {
/**
* An object representing the user's data loaded from the dabase.
*
* @var stdClass
*/
protected $data;
/**
* Determines if this type of user is an administrator.
*
* @var boolean
*/
protected $_isAdmin = false;
/**
* Determines if this type of user is an administrator.
*
* @var boolean
*/
protected $_isPremium = false;
/**
* Constructs a new user object.
* The $userid should correspond to the primary key
* of the users table.
*
* @param integer $userid
*/
public function __construct($userid) {
// Define a query string that will load the user's data.
$q_get = sprintf('select * from users where userid = %d', intval($userid));
// Load the user's data
$result = $this->db->query($q_get);
// Fetch that data in the form of an object (stdClass).
$result = $result->fetchObject();
// No need to have the password in the user object.
unset($result->password);
$this->data = $result;
}
/**
* Get an HTML link to the user's profile.
* This link is styled according to whether
* the user is a regular user, a premium user,
* or an administrator.
*
* @return string
*/
public function getProfileLink() {
return sprintf('<a href="#userid=%d" class="userProfileLink">%s</a>', $this->data->userid, $this->data->name);
}
/**
* Is the user an administrator?
*
* @return boolean
*/
public function isAdmin() {
return $this->_isAdmin;
}
/**
* Is the user a Premium member?
*
* @return boolean
*/
public function isPremium() {
return $this->_isPremium;
}
}
<?php
class userAdmin extends userPremium {
protected $_isAdmin = true;
public function getProfileLink() {
return sprintf('<a href="#userid=%d" class="adminProfileLink">%s</a>', $this->data->userid, $this->data->name);
}
}
<?php
class userPremium extends user {
protected $_isPremium = true;
public function getProfileLink() {
return sprintf('<a href="#userid=%d" class="premiumProfileLink">%s</a>', $this->data->userid, $this->data->name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment