ddc
联网
平面设计 画册 VI欣赏 包装 CG-插画 搜索 个人网页 Alexa排名 CSS 建站资源 下载专区 JS特效 品牌服装 服装院校 专题欣赏 SEO 图标欣赏 专题
网站建设 域名注册 网站建设 虚拟主机 广州网站设计 域名注册 广州网站建设 上海网站建设 虚拟主机 广州网页设计 虚拟主机 域名注册 acg王国 ACG玩家 品牌设计 上海网站建设
求创科技
网站建设
中国互联
素材出售
中国设计秀
中资源
当前位置:网络学院首页 >> 编程开发 >> php >> 在数据库中使用对象有那些好处?

在数据库中使用对象有那些好处? (2)

来源:中国设计秀    作者:    点击:56     加入收藏    发表评论
0
顶一下

这类方法被转移到被称作记录(Record)的抽象的超级类(super class),当然它是User类下的子类
这个记录类(Record class)掌握所有存取数组 $_data的细节,在内容被修改之前调用验证的方法,以及将变更的通知发给记录(Records),就像发给中心对象存储(ObjectStore)实例。
 
<?php
class User extends Record {

  // --- OMITTED CODE --- //

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

[1] [2] [3]
2007-08-01 10:33:00    出处:
Google
热点文章/相关文章
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明 | 网站公告 | 友情链接 | 留言 | 旧版入口