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

歡迎訪問 生活随笔!

生活随笔

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

php

PHP Collection 类

發布時間:2024/4/14 php 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP Collection 类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?php /*** PHP collection 類(一) */ error_reporting(0); class Collection{protected $_members = array();public function addItem($obj,$key = null){if(empty($key)) throw new Exception("The key is not given!");if(isset($this->_members[$key])) throw new Exception("The key is exists");$this->_members[$key] = $obj;}public function removeItem($key){if(!isset($this->_members[$key])) throw new Exception("Invalid key");unset($this->_members[$key]);}public function getItem($key){if(!isset($this->_members[$key])) throw new Exception("Invalid key");return $this->_members[$key];}public function getLength(){return sizeof($this->_members);}public function keys(){return array_keys($this->_members);} } class person{private $_name;private $_number;public function __construct($name,$number){$this->_name = $name;$this->_number = $number;}public function __toString(){return $this->_name.' is '.$this->_number;} } $testC = new superCollection(); $testC->spaddItem(new person('xiaojiang',25),'xiaojiang'); $testC->spaddItem(new person('xiaoxin',25),'xiaoxin'); //$testC->removeItem('xiaojiang'); //print_r($testC); /*try{// var_dump($testC);//echo $testC->spgetItem('xiaojiang'); }catch(Exception $e){echo $e->getmessage(); } */class superCollection extends Collection{private $_isLoaded = false;private $_onLoad;public function __call($method,$param){if(substr($method,0,2) == 'sp' && is_callable( array(parent,substr($method,2)) ) ){$this->_checkCallback();return call_user_func_array(array(parent,substr($method,2)),$param);}}public function _checkCallback(){if(!$_isLoaded && isset($this->_onLoad)){$this->isLoaded = true;call_user_func($this->_onLoad,$this);}}public function setCallback($funName,$Obj = null,$param){if($Obj){$callback = array($Obj,$funName);}else{$callback = $funName;}if(!is_callable($callback,false,$callbackName)){throw new Exception("{$callbackName} can't be called");return false;}$this->_onLoad = $callback;} }class course_collection extends superCollection{} class course{private $_name;private $_course_code;private $_id;function __construct($name,$code,$id){$this->_name = $name;$this->_course_code = $code;$this->_id = $id;}public function getId(){return $this->_id;} } class student{private $_name;private $_age;public function __construct($name,$age){$this->_name = $name;$this->_age = $age;$this->_course = new course_collection();$this->_course->setCallback('getCourse',$this);}public function getCourse($courseObj){$date = array('xiaojiang'=> array(array('id'=>1,'name'=>'math','code'=>99),array('id'=>2,'name'=>'english','code'=>88)),'xiaoxin'=> array(array('id'=>3,'name'=>'math','code'=>77),array('id'=>4,'name'=>'english','code'=>66)));foreach($date[$this->_name] as $k => $v){$course = new course($v['name'],$v['code'],$v['id']);$courseObj->addItem($course,$course->getId());}}public function getName(){return $this->_name;}} try{$Stud = new student('xiaojiang',25);//echo $Stud->getName();print_r($Stud->_course->spgetItem(2)); }catch(Exception $e){echo $e->getmessage(); } ?>

轉載于:https://www.cnblogs.com/glory-jzx/archive/2012/08/01/2617613.html

總結

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

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