加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

PHP面向工具之规模模子+数据映射器实例(说明)

发布时间:2021-01-24 23:39:17 所属栏目:编程 来源:网络整理
导读:这里要声名一下 由于本人较量懒 博客中相干文章的内容更多的是对 一书中代码的清算和简朴注解利便本身日后温习和参考, 对相干内容感乐趣的初学的伴侣提议请先阅读原文。此处的内容只能当成一种进修的增补和参考。感谢! 因原书中规模模子+数据映射器的示例

下面这个类是处理赏罚多行记录的,转达数据库中取出的原始数据和映射器进去,然后通过数据映射器在获取数据时将其建设成工具

private $result;
private $pointer = 0; //指针
private $objects = array(); //工具荟萃

function __construct (array $raw = null,Mapper $mapper= null){
if(!is_null($raw)&& !is_null($mapper)){
$this->raw = $raw;
$this->total = count($raw);
}
$this->mapper = $mapper;
}

function add(woodomainDmainObject $object){ //这里是直接添加工具
$class = $this->targetClass();
if(!($object instanceof $class)){
throw new Exception("This is a {$class} collection");
}
$this->notifyAccess();
$this->objects[$this->total] = $object;
$this->total ++;
}

abstract function targetClass(); //子类中实现用来在插入工具时搜查范例的

protected function notifyAccess(){ //不知道干嘛的

}

private function getRow($num){ //获取荟萃中的单条数据,就是这里通过数据映射器将数据建设成工具
$this->notifyAccess();
if($num >= $this->total || $num < 0){
return null;
}
if(isset($this->objects[$num]){
return $this->objects[$num];
}
if(isset($this->raw[$num]){
$this->objects[$num] = $this->mapper->createObject($this->raw[$num]);
return $this->objects[$num];
}
}

public function rewind(){ //重置指针
$this->pointer = 0;
}

public function current(){ //获取当前指针工具
return $this->getRow($this->pointer);
}

public function key(){ //获取当前指针
return $this->pointer;
}

public function next(){ //获取当前指针工具,并将指针下移
$row = $this->getRow($this->pointer);
if($row){$this->pointer ++}
return $row;
}

public function valid(){ //验证
return (!is_null($this->current()));
}

}

//子类
class VenueColletion extends Collection implements woodomainVenueCollection{
function targetClass(){
return "woodomainVenue";
}
}

//客户端
$mapper = new woomapperVenueMapper();
$venue = $mapper->find(12);
print_r($venue);

$venue = new woodomainVenue();
$venue->setName("the likey lounge-yy");
//插入工具到数据库
$mapper->insert($venue);
//从数据库中读出适才插入的工具
$venue = $mapper->find($venue->getId());
print_r($venue);

//修改工具
$venue->setName("the bibble beer likey lounge-yy");
//挪用update来更新记录
$mapper->update($venue);
//再次读出工具数据
$venue = $mapper->find($venue->getId());
print_r($venue);

//竣事

以上这篇PHP面向工具之规模模子+数据映射器实例(说明)就是小编分享给各人的所有内容了,但愿能给各人一个参考,也但愿各人多多支持编程之家。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读