PHP Collection 类
生活随笔
收集整理的這篇文章主要介紹了
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 类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #13
- 下一篇: PHP如何添加变量 $_SERVER