Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4997873
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类:

2008-03-05 11:01:31

我们都知道如何从mysql获取我们需要的行(记录),读取数据,然后存取一些改动。很明显也很直接,在这个过程背后也没有什么拐弯抹角的。然而对于我们使用面对对象的程序设计(oop)来管理我们数据库中的数据时,这个过程就需要大大改进一下了。这篇文章将对如何设计一个面对对象的方式来管理数据库的记录做一个简单的描述。你的数据当中的所有内部逻辑关系将被封装到一个非常条理的记录对象,这个对象能够提供专门(专一)的确认代码系统,转化以及数据处理。随着zend engine2 和php5的发布,php开发者将会拥有更强大的面对对象的工具来辅助工作,这将使这个过程(面对对象地管理数据库)更有吸引力。

以下列出了一些使用对象来描叙你的数据库的有利方面:

存取方法(accessor methods)将会使你对属性的读取和写入过程做到完全的控制
每一级的每个记录和属性(的操作)都有确认过程
从关系表中智能的获取对象
重复使用的逻辑方法意味着所有的数据交互都要通过相同的基础代码(codebase),这将使维护变得更加简单
代码简单,因为不同的记录的内部逻辑都已经包含在各自所处的类(class)当中,而不是繁琐的库(lib)文件
在手工编写代码和sql查询语句时,出错的机会将更少

存取方法(accessor methods)
存取方式是通过类给实例(instance)的变量赋值。一个例子,我有一个叫user的类,并且有一个实例$username,我会写这样的存取方法(函数),user->username()和user->setusername()用来返回和给实例赋值。
 
class user {
var $username;
function username() {
return $this->username;
}
function setusername($newusername) {
$this->username = $newusername;
}
}
?>
 

这里有很好的理由让我们编写这样的“特别的代码”。它将使开发者更灵活的改变类的繁琐的工作,因为这一过程将不需要其他的使用类的php代码。让我们来看看下面这个更加完善的可信赖的user类。

变量$username将不复存在,所有的东西都被整合的放在数组$_data当中
如果username是空的话,username()函数将提供一个缺省(默认)的值给它
setusername()过程将在接受值之前确认username是否合乎标准格式(如字长等)
class user {
var $_data = array(); // associative array containing all the attributes for the user
function username() {
return !empty($this->_data[username]) ? $this->_data[username] : (no name!);
}
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;
}
}
?>
 
显而易见,这对我们控制存取对象的数据有很大帮助。如果一个程序员已经直接地存取username的信息,以上代码的变化将会破坏他的代码。然而我们可以使用(类的)存取方法,就像上面代码中注释的那样,添加一个验证的功能而不需要改变任何其他的东西。注意username的验证(例子当中是不能超过12字节)代码是独立在setusername()方法之外的。从验证到存储到数据库的过程轻而易举。而且,这是个非常好的单凭经验的方法,一个方法或一个类需要做的越少,它的重复使用的机会将会越大。这在你开始写一个子类时更加明显,假如你需要一个子类,并且又要跳过(忽略)父类方法(行为)中的一些特殊的细节,如果(针对这个细节的)方法很小而又精细,(修改它)只是一瞬间的过程,而如果这个方法非常臃肿,针对多种目的,你可能将在复制子类中大量代码中郁闷而终。
比方说,假如admin是user类的一个子类。我们对adamin的用户可能会有不同的,相对苛刻一些的密码验证方法。最好是跨过父类的验证方法和整个setusername()方法(在子类中重写)。
更多关于存取器(accessor)
下面是一些其他的例子来说明如何使存取器用的更有效果。很多时候我们可能要计算结果,而不是简单的返回数组中的静态数据。存取方法还能做的一个有用的事情就是更新(updating)缓存中的值。当所有的变动(对数据的所有操作)都要通过setx()方法的时候,这正是我们根据x来重置缓存中的值的时刻。
于是我们的这个类层次变得更加明了:
内部变量$_data的处理被替换成受保护的私有方法(private methods)_getdata()和_setdata()
这类方法被转移到被称作记录(record)的抽象的超级类(super class),当然它是user类下的子类
这个记录类(record class)掌握所有存取数组$_data的细节,在内容被修改之前调用验证的方法,以及将变更的通知发给记录(records),就像发给中心对象存储(objectstore)实例。
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 users 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
}
}
?>
 
现在我们拥有了一个抽象的超级类(record),我们可以将user类里面大量的代码转移出来,而让这个user的子类来关注user的特殊项目如存取和验证方法。你可能已经注意到在我们的这个纪录类(record class)没有任何的sql代码。这并不是疏忽或者遗漏!对象存储类(objectstore class)(隐藏在第二部分)将负责所有和数据库的交互,还有我们的超级类record的实例化。这样使我们的record类更加瘦小而又有效率,而这对于评价我们处理大量对象的效率的时候是个重要因素。
阅读(1290) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~