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 } } ?>