天天站长

 找回密码
 用户注册
查看: 106848|回复: 1

在数据库中使用对象的好处

[复制链接]

0

主题

0

听众

9633

积分

核心会员

Rank: 8Rank: 8

UID
29
积分
9633
威望
1799
金币
3606
贡献
60
活跃
0
魅力
0
注册时间
2007-3-22

论坛建设奖

发表于 2007-5-24 00:40:09 |显示全部楼层
&nbsp;&nbsp;&nbsp;&nbsp;<FONT size=2>&lt;?php<BR>class User extends Record {<BR>// --- OMITTED CODE --- //<BR>/**<BR>* Do not show the actual password for the user, only some asterixes with the same strlen as the password value.<BR>*/<BR>function password() {<BR>   $passLength = strlen($this-&gt;_getData('password'));<BR>   return str_repeat('*', $passLength);<BR>}<BR>/**<BR>* Setting the user password is not affected.<BR>*/<BR>function setPassword($newPassword) {<BR>   $this-&gt;_setData('password', $newPassword);<BR>}<BR>/**<BR>* fullName is a derived attribute from firstName and lastName<BR>* and does not need to be stored as a variable.<BR>* It is therefore read-only, and has no 'setFullname()' accessor method.<BR>*/<BR>function fullName() {<BR>   return $this-&gt;firstName() . " " . $this-&gt;lastName();<BR>}<BR>/**<BR>* Spending limit returns the currency value of the user's spending limit.<BR>* This value is stored as an INT in the database, eliminating the need<BR>* for more expensive DECIMAL or DOUBLE column types.<BR>*/<BR>function spendingLimit() {<BR>   return $this-&gt;_getData('spendingLimit') / 100;<BR>}<BR>/**<BR>* The set accessor multiplies the currency value by 100, so it can be stored in the database again<BR>* as an INT value.<BR>*/<BR>function setSpendingLimit($newSpendLimit) {<BR>   $this-&gt;_setData('spendingLimit', $newSpendLimit * 100);<BR>}<BR>/**<BR>* The validateSpendingLimit is not called in this class, but is called automatically by the _setData() method<BR>* in the Record superclass, which in turn is called by the setSpendingLimit() method.<BR>*/<BR>function validateSpendingLimit(&amp;$someLimit) {<BR>   if (is_numeric($someLimit) AND $someLimit &gt;= 0) {<BR>     return true;<BR>   } else {<BR>     throw new Exception("Spending limit must be a non-negative integer"); //PHP5 only<BR>   }<BR>}<BR>}<BR>/**<BR>* Record is the superclass for all database objects.<BR>*/<BR>abstract class Record {<BR>var $_data = array();<BR>var $_modifiedKeys = array(); // keeps track of which fields have changed since record was created/fetched<BR>/**<BR>* Returns an element from the $_data associative array.<BR>*/<BR>function _getData($attributeName) {<BR>   return $this-&gt;_data[$attributeName];<BR>}<BR>/**<BR>* If the supplied value passes validation, this<BR>* sets the value in the $_data associative array.<BR>*/<BR>function _setData($attributeName, $value) {<BR>   if ($this-&gt;validateAttribute($attributeName, $value)) {<BR>     if ($value != $this-&gt;_data[$attributeName]) {<BR>       $this-&gt;_data[$attributeName] = $value;<BR>       $this-&gt;_modifiedKeys[] = $attributeName;<BR>       $this-&gt;didChange();<BR>     } else {<BR>       // the new value is identical to the current one<BR>       // no change necessary<BR>     }<BR>   }<BR>}<BR>/**<BR>* For an attribute named "foo", this looks for a method named "validateFoo()"<BR>* and calls it if it exists. Otherwise this returns true (meaning validation passed).<BR>*/<BR>function validateAttribute($attributeName, &amp;$value) {<BR>   $methodName = 'validate' . $attributeName;<BR>   if (method_exists($this, $methodName)) {<BR>     return $this-&gt;$methodName($value);<BR>   } else {<BR>     return true;<BR>   }<BR>}<BR>function didChange() {<BR>   // notify the objectStore that this record changed<BR>}<BR>}<BR>?&gt;现在我们拥有了一个抽象的超级类(Record),我们可以将User类里面大量的代码转移出来,而让这个User的子类来关注User的特殊项目如存取和验证方法。你可能已经注意到在我们的这个纪录类(Record class)没有任何的SQL代码。这并不是疏忽或者遗漏!对象存储类(ObjectStore class)(隐藏在第二部分)将负责所有和数据库的交互,还有我们的超级类Record的实例化。这样使我们的Record类更加瘦小而又有效率,而这对于评价我们处理大量对象的效率的时候是个重要因素。</FONT>                                                         

0

主题

0

听众

1016

积分

黄金会员

Rank: 4

UID
60080
积分
1016
威望
0
金币
675
贡献
0
活跃
2
魅力
1
注册时间
2013-12-23
发表于 2013-12-23 21:33:23 |显示全部楼层
看帖子的要发表下看法
[url=http://www.http://www.jsjlbbs.com/[b]建设交流论坛[/b][/url]
[url=http://www.http://www.jsjlbbs.com/][b]施工组织设计[/b][/url]&amp;lt;strong&amp;gt;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 用户注册


|申请友情链接|关于我们|商家合作|广告报价|联系我们|法律声明|隐私条款|无图浏览|手机浏览|  


敬请您尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其它各项有关法律法规。

本网所有帖子均来源网友上传,本站未进行任何修改和未获取任何利益,且仅代表作者本人意见,不代表本网站立场。

天天站长 技术支持:杭州桐庐家园网络技术有限公司 广告合作:13968063988 在线QQ:18118865

Copyright ©2003-2012 Powered by 安网数据 All Rights Reserved. 备案信息:浙B2-20060153号