日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php Collection类的设计

發布時間:2023/12/9 php 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php Collection类的设计 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用。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 validif(!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);

?

轉載于:https://www.cnblogs.com/majiang/archive/2012/11/23/2783779.html

總結

以上是生活随笔為你收集整理的php Collection类的设计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。