相思资源网 Design By www.200059.com
用.net开发已经很多年了,最近接触到php,发现php也很好玩。不过发现它里面没有集合Collection类,只有数组,并且数组很强。这里我用数组来包装成一个集合Collection,代码如下:
复制代码 代码如下:
class Collection{
private $_members=array();
public function addItem($obj,$key=null)
{
if($key)
{
if(isset($this->_members[$key]))
{
throw new exception("Key \"$key\" already in use!");
}
else
{
$this->_members[$key]=$obj;
}
}
else
{
$this->_members[]=$obj;
}
}
public function removeItem($key)
{
if(isset($this->_members[$key]))
{
unset($this->_members[$key]);
}
else
{
throw new exception("Invalid Key \"$key\"!");
}
}
public function getItem($key)
{
if(isset($this->_members[$key]))
{
return $this->_members[$key];
}
else
{
throw new exception("Invalid Key \"$key\"!");
}
}
public function Keys()
{
return array_keys($this->_members);
}
public function legth()
{
return sizeof($this->_members);
}
public function exists($key)
{
return (isset($this->_members[$key]));
}
}
现在我们来测试一下这个集合是否好用。
我们首先建立一个集合元素类Course:
复制代码 代码如下:
class Course
{
private $_id;
private $_courseCode;
private $_name;
public function __construct($id,$courseCode,$name)
{
$this->_id=$id;
$this->_courseCode=$courseCode;
$this->_name=$name;
}
public function getName()
{
return $this->_name;
}
public function getID()
{
return $this->_id;
}
public function getCourseCode()
{
return $this->_courseCode;
}
public function __toString()
{
return $this->_name;
}
}
测试代码如下:
$courses=new Collection();
$courses->addItem(new Course(1, "001", "语文"),1);
$courses->addItem(new Course(2, "002", "数学"),2);
$obj=$courses->getItem(1);
print $obj;
我想这个集合类应该可以满足我们平日开发的需求了吧。
可是我们现在。net里面有个对象延迟加载,举个例子来说吧,假如现在有Student这个对象,它应该有很多Course,但是我们希望在访问Course之前Course是不会加载的。也就是说在实例化Student的时候Course个数为0,当我们需要Course的时候它才真正从数据库读取相应数据。就是需要我们把Collection做成惰性实例化。
修改后的Collection代码如下:
复制代码 代码如下:
class Collection {
private $_members = array(); //collection members
private $_onload; //holder for callback function
private $_isLoaded = false; //flag that indicates whether the callback
//has been invoked
public function addItem($obj, $key = null) {
$this->_checkCallback(); //_checkCallback is defined a little later
if($key) {
if(isset($this->_members[$key])) {
throw new KeyInUseException("Key \"$key\" already in use!");
} else {
$this->_members[$key] = $obj;
}
} else {
$this->_members[] = $obj;
}
}
public function removeItem($key) {
$this->_checkCallback();
if(isset($this->_members[$key])) {
unset($this->_members[$key]);
} else {
throw new KeyInvalidException("Invalid key \"$key\"!");
}
}
public function getItem($key) {
$this->_checkCallback();
if(isset($this->_members[$key])) {
return $this->_members[$key];
} else {
throw new KeyInvalidException("Invalid key \"$key\"!");
}
}
public function keys() {
$this->_checkCallback();
return array_keys($this->_members);
}
public function length() {
$this->_checkCallback();
return sizeof($this->_members);
}
public function exists($key) {
$this->_checkCallback();
return (isset($this->_members[$key]));
}
/**
* Use this method to define a function to be
* invoked prior to accessing the collection.
* The function should take a collection as a
* its sole parameter.
*/
public function setLoadCallback($functionName, $objOrClass = null) {
if($objOrClass) {
$callback = array($objOrClass, $functionName);
} else {
$callback = $functionName;
}
//make sure the function/method is valid
if(!is_callable($callback, false, $callableName)) {
throw new Exception("$callableName is not callable " .
"as a parameter to onload");
return false;
}
$this->_onload = $callback;
}
/**
* Check to see if a callback has been defined and if so,
* whether or not it has already been called. If not,
* invoke the callback function.
*/
private function _checkCallback() {
if(isset($this->_onload) && !$this->_isLoaded) {
$this->_isLoaded = true;
call_user_func($this->_onload, $this);
}
}
}
所需的Student如下:
复制代码 代码如下:
class CourseCollection extends Collection {
public function addItem(Course $obj,$key=null) {
parent::addItem($obj,$key);
}
}
class Student{
private $_id;
private $_name;
public $course;
public function __construct($id,$name)
{
$this->_id=$id;
$this->_name=$name;
$this->course=new CourseCollection();
$this->course->setLoadCallback('loadCourses',$this);
}
public function getName()
{
return $this->_name;
}
public function getID()
{
return $this->_id;
}
public function __toString()
{
return $this->_name;
}
public function loadCourses(Collection $col)
{
$col->addItem(new Course(1, "001", "语文"),1);
$col->addItem(new Course(2, "002", "数学"),2);
}
}
调用代码如下:
$student=new Student(1, "majiang");
print $student->getName();
print $student->course->getItem(1);
复制代码 代码如下:
class Collection{
private $_members=array();
public function addItem($obj,$key=null)
{
if($key)
{
if(isset($this->_members[$key]))
{
throw new exception("Key \"$key\" already in use!");
}
else
{
$this->_members[$key]=$obj;
}
}
else
{
$this->_members[]=$obj;
}
}
public function removeItem($key)
{
if(isset($this->_members[$key]))
{
unset($this->_members[$key]);
}
else
{
throw new exception("Invalid Key \"$key\"!");
}
}
public function getItem($key)
{
if(isset($this->_members[$key]))
{
return $this->_members[$key];
}
else
{
throw new exception("Invalid Key \"$key\"!");
}
}
public function Keys()
{
return array_keys($this->_members);
}
public function legth()
{
return sizeof($this->_members);
}
public function exists($key)
{
return (isset($this->_members[$key]));
}
}
现在我们来测试一下这个集合是否好用。
我们首先建立一个集合元素类Course:
复制代码 代码如下:
class Course
{
private $_id;
private $_courseCode;
private $_name;
public function __construct($id,$courseCode,$name)
{
$this->_id=$id;
$this->_courseCode=$courseCode;
$this->_name=$name;
}
public function getName()
{
return $this->_name;
}
public function getID()
{
return $this->_id;
}
public function getCourseCode()
{
return $this->_courseCode;
}
public function __toString()
{
return $this->_name;
}
}
测试代码如下:
$courses=new Collection();
$courses->addItem(new Course(1, "001", "语文"),1);
$courses->addItem(new Course(2, "002", "数学"),2);
$obj=$courses->getItem(1);
print $obj;
我想这个集合类应该可以满足我们平日开发的需求了吧。
可是我们现在。net里面有个对象延迟加载,举个例子来说吧,假如现在有Student这个对象,它应该有很多Course,但是我们希望在访问Course之前Course是不会加载的。也就是说在实例化Student的时候Course个数为0,当我们需要Course的时候它才真正从数据库读取相应数据。就是需要我们把Collection做成惰性实例化。
修改后的Collection代码如下:
复制代码 代码如下:
class Collection {
private $_members = array(); //collection members
private $_onload; //holder for callback function
private $_isLoaded = false; //flag that indicates whether the callback
//has been invoked
public function addItem($obj, $key = null) {
$this->_checkCallback(); //_checkCallback is defined a little later
if($key) {
if(isset($this->_members[$key])) {
throw new KeyInUseException("Key \"$key\" already in use!");
} else {
$this->_members[$key] = $obj;
}
} else {
$this->_members[] = $obj;
}
}
public function removeItem($key) {
$this->_checkCallback();
if(isset($this->_members[$key])) {
unset($this->_members[$key]);
} else {
throw new KeyInvalidException("Invalid key \"$key\"!");
}
}
public function getItem($key) {
$this->_checkCallback();
if(isset($this->_members[$key])) {
return $this->_members[$key];
} else {
throw new KeyInvalidException("Invalid key \"$key\"!");
}
}
public function keys() {
$this->_checkCallback();
return array_keys($this->_members);
}
public function length() {
$this->_checkCallback();
return sizeof($this->_members);
}
public function exists($key) {
$this->_checkCallback();
return (isset($this->_members[$key]));
}
/**
* Use this method to define a function to be
* invoked prior to accessing the collection.
* The function should take a collection as a
* its sole parameter.
*/
public function setLoadCallback($functionName, $objOrClass = null) {
if($objOrClass) {
$callback = array($objOrClass, $functionName);
} else {
$callback = $functionName;
}
//make sure the function/method is valid
if(!is_callable($callback, false, $callableName)) {
throw new Exception("$callableName is not callable " .
"as a parameter to onload");
return false;
}
$this->_onload = $callback;
}
/**
* Check to see if a callback has been defined and if so,
* whether or not it has already been called. If not,
* invoke the callback function.
*/
private function _checkCallback() {
if(isset($this->_onload) && !$this->_isLoaded) {
$this->_isLoaded = true;
call_user_func($this->_onload, $this);
}
}
}
所需的Student如下:
复制代码 代码如下:
class CourseCollection extends Collection {
public function addItem(Course $obj,$key=null) {
parent::addItem($obj,$key);
}
}
class Student{
private $_id;
private $_name;
public $course;
public function __construct($id,$name)
{
$this->_id=$id;
$this->_name=$name;
$this->course=new CourseCollection();
$this->course->setLoadCallback('loadCourses',$this);
}
public function getName()
{
return $this->_name;
}
public function getID()
{
return $this->_id;
}
public function __toString()
{
return $this->_name;
}
public function loadCourses(Collection $col)
{
$col->addItem(new Course(1, "001", "语文"),1);
$col->addItem(new Course(2, "002", "数学"),2);
}
}
调用代码如下:
$student=new Student(1, "majiang");
print $student->getName();
print $student->course->getItem(1);
标签:
PHP,Collection
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无浅析PHP中Collection 类的设计的评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。