浅析PHP中Collection 类的设计
程序员文章站
2022-05-25 17:28:50
用.net开发已经很多年了,最近接触到php,发现php也很好玩。不过发现它里面没有集合collection类,只有数组,并且数组很强。这里我用数组来包装成一个集合coll...
用.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);