淺析PHP中Collection 類的設(shè)計(jì)
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]));
}
}
現(xiàn)在我們來測(cè)試一下這個(gè)集合是否好用。
我們首先建立一個(gè)集合元素類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;
}
}
測(cè)試代碼如下:
$courses=new Collection();
$courses->addItem(new Course(1, "001", "語文"),1);
$courses->addItem(new Course(2, "002", "數(shù)學(xué)"),2);
$obj=$courses->getItem(1);
print $obj;
我想這個(gè)集合類應(yīng)該可以滿足我們平日開發(fā)的需求了吧。
可是我們現(xiàn)在。net里面有個(gè)對(duì)象延遲加載,舉個(gè)例子來說吧,假如現(xiàn)在有Student這個(gè)對(duì)象,它應(yīng)該有很多Course,但是我們希望在訪問Course之前Course是不會(huì)加載的。也就是說在實(shí)例化Student的時(shí)候Course個(gè)數(shù)為0,當(dāng)我們需要Course的時(shí)候它才真正從數(shù)據(jù)庫讀取相應(yīng)數(shù)據(jù)。就是需要我們把Collection做成惰性實(shí)例化。
修改后的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", "數(shù)學(xué)"),2);
}
}
調(diào)用代碼如下:
$student=new Student(1, "majiang");
print $student->getName();
print $student->course->getItem(1);
相關(guān)文章
php 去除html標(biāo)記--strip_tags與htmlspecialchars的區(qū)別詳解
本篇文章是對(duì)php中去除html標(biāo)記以及strip_tags與htmlspecialchars的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP iconv 函數(shù)轉(zhuǎn)gb2312的bug解決方法
之前寫過一篇解決xajax庫中文亂碼的問題,說到可以用 iconv( "UTF-8", "gb2312" , $FormValues['a']) 來轉(zhuǎn)換,最近發(fā)現(xiàn)更好的寫法應(yīng)該是。2009-10-10php進(jìn)程(線程)通信基礎(chǔ)之System V共享內(nèi)存簡單實(shí)例分析
這篇文章主要介紹了php進(jìn)程(線程)通信基礎(chǔ)之System V共享內(nèi)存,結(jié)合簡單實(shí)例形式分析了PHP System V共享內(nèi)存原理、相關(guān)函數(shù)與基本使用技巧,需要的朋友可以參考下2019-11-11php合并數(shù)組array_merge函數(shù)運(yùn)算符加號(hào)與的區(qū)別
“+”運(yùn)算符和array_merge():array array_merge ( array array1, array array2 [, array ...] ) 都可以合并多個(gè)數(shù)組,但使用過程中有一點(diǎn)小區(qū)別。2008-10-10PHP實(shí)現(xiàn)冒泡排序的簡單實(shí)例
下面小編就為大家?guī)硪黄狿HP實(shí)現(xiàn)冒泡排序的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05PHP根據(jù)樹的前序遍歷和中序遍歷構(gòu)造樹并輸出后序遍歷的方法
這篇文章主要介紹了PHP根據(jù)樹的前序遍歷和中序遍歷構(gòu)造樹并輸出后序遍歷的方法,涉及php數(shù)據(jù)結(jié)構(gòu)與算法中關(guān)于數(shù)的遍歷相關(guān)操作技巧,需要的朋友可以參考下2017-11-11學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)門面模式(Facade)
這篇文章主要介紹了php設(shè)計(jì)模式中的門面模式,使用php實(shí)現(xiàn)門面模式,感興趣的小伙伴們可以參考一下2015-12-12php中判斷文件空目錄是否有讀寫權(quán)限的函數(shù)代碼
有時(shí)候我們需要判斷是否對(duì)目錄有讀寫權(quán)限就需要下面的代碼,需要的朋友的可以參考下2012-08-08淺談ThinkPHP5.0版本和ThinkPHP3.2版本的區(qū)別
小編小編就為大家?guī)硪黄獪\談ThinkPHP5.0版本和ThinkPHP3.2版本的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06