function setUsername($newUsername) { if ($this->validateUsername($newUsername)) { $this->_data['username'] = $newUsername; } }
function validateUsername(&$someName) { if (strlen($someName) > 12) { throw new Exception('Your username is too long'); // PHP5 only } return true; } } ?>
/** * Do not show the actual password for the user, only some asterixes with the same strlen as the password value. */ function password() { $passLength = strlen($this->_getData('password')); return str_repeat('*', $passLength); } /** * Setting the user password is not affected. */ function setPassword($newPassword) { $this->_setData('password', $newPassword); }
/** * fullName is a derived attribute from firstName and lastName * and does not need to be stored as a variable. * It is therefore read-only, and has no 'setFullname()' accessor method. */ function fullName() { return $this->firstName() . " " . $this->lastName(); }
/** * Spending limit returns the currency value of the user's spending limit. * This value is stored as an INT in the database, eliminating the need * for more expensive DECIMAL or DOUBLE column types. */ function spendingLimit() { return $this->_getData('spendingLimit') / 100; }
/** * The set accessor multiplies the currency value by 100, so it can be stored in the database again * as an INT value. */ function setSpendingLimit($newSpendLimit) { $this->_setData('spendingLimit', $newSpendLimit * 100); }
/** * The validateSpendingLimit is not called in this class, but is called automatically by the _setData() method * in the Record superclass, which in turn is called by the setSpendingLimit() method. */ function validateSpendingLimit(&$someLimit) { if (is_numeric($someLimit) AND $someLimit >= 0) { return true; } else { throw new Exception("Spending limit must be a non-negative integer"); //PHP5 only } } }
/** * Record is the superclass for all database objects. */ abstract class Record { var $_data = array(); var $_modifiedKeys = array(); // keeps track of which fields have changed since record was created/fetched
/** * Returns an element from the $_data associative array. */ function _getData($attributeName) { return $this->_data[$attributeName]; }
/** * If the supplied value passes validation, this * sets the value in the $_data associative array. */ function _setData($attributeName, $value) { if ($this->validateAttribute($attributeName, $value)) { if ($value != $this->_data[$attributeName]) { $this->_data[$attributeName] = $value; $this->_modifiedKeys[] = $attributeName; $this->didChange(); } else { // the new value is identical to the current one // no change necessary } } }
/** * For an attribute named "foo", this looks for a method named "validateFoo()" * and calls it if it exists. Otherwise this returns true (meaning validation passed). */ function validateAttribute($attributeName, &$value) { $methodName = 'validate' . $attributeName; if (method_exists($this, $methodName)) { return $this->$methodName($value); } else { return true; } }
function didChange() { // notify the objectStore that this record changed } } ?>