An Simple Collection Class
2009-02-17 15:01
239 查看
Construct an Collection:
/**
* @copyright 2009-1-16
* @author Tom Zhou MSN:danni-505@hotmail.com
* @Info: Use ArrayObject to build an super simple collection,
* it can be used to store various of data, such as DB connection,Object etc.
*
* @access anything
* @return data of corresponding opposite
*/
class MySimpleCollection extends ArrayObject{
/**
* if identity private variable, in construct you should call parent construct and set the
* instance to this private variable.
* antoher way is never override this construct method
*/
// private $collection;
// function __construct(){
// parent::__construct();
// $this->collection=$this;
// }
function addObject($_index,$_object){
$this->offsetSet($_index,$_object);
}
function getObject($_index){
return $this->offsetGet($_index);
}
function delObject($_index){
$this->offsetUnset($_index);
}
function printCollection(){
echo "<pre>";
print_r($this);
echo "</pre>";
}
}
Store some data
//#get some object
$user=new userObject(); //#general object
$dao=new userDao(); //#DB object
//#put into collection
$colletionInstance=new MySimpleCollection();
$colletionInstance->addObject(1,$user);
$colletionInstance->addObject(2,$dao->dbObjInstance);
$colletionInstance->addObject(3,$_SESSION["URL_FROM"]);
$colletionInstance->addObject(4,serialize("daniel"));
$colletionInstance->addObject(5,unserialize(serialize("daniel")));
$colletionInstance->addObject(6,array(0=>"string",1=>array("name"=>"daniel"),"last name"=>"zhou"));
$colletionInstance->addObject(7,array(0=>"daniel"));
$colletionInstance->addObject(8,0.2356);
$colletionInstance->addObject(9,"daniel");
//#have a look
$colletionInstance->printCollection();
$iterater=$colletionInstance->getIterator();
while ($iterater->valid()) {
echo $iterater->current()."<br>";
echo $iterater->key()."<BR>";
$iterater->next();
}
then take a look
MySimpleCollection Object
(
[1] => user Object
(
[no] => 1
)
[2] => dbObjMySQL Object
(
[db_host] => 192.168.1.166
[db_name] => LOAN163
[user_name] => root
[pass_code] => 2008like18
[db_connect] => Resource id #22
[db_rec] =>
[ErrMsg] =>
[PHPErrCd] =>
[PHPErrMsg] =>
[PHPSQL] =>
[tran_flg] =>
[new_link] =>
)
[3] => /backyard/?Command=Admin_Login
[4] => s:6:"daniel";
[5] => daniel
[6] => Array
(
[0] => string
[1] => Array
(
[name] => daniel
)
[last name] => zhou
)
[7] => Array
(
[0] => daniel
)
[8] => 0.2356
[9] => daniel
)
Object id #6
1
Object id #10
2
/backyard/?Command=Admin_Login
3
s:6:"daniel";
4
daniel
5
Array
6
Array
7
0.2356
8
daniel
9
/**
* @copyright 2009-1-16
* @author Tom Zhou MSN:danni-505@hotmail.com
* @Info: Use ArrayObject to build an super simple collection,
* it can be used to store various of data, such as DB connection,Object etc.
*
* @access anything
* @return data of corresponding opposite
*/
class MySimpleCollection extends ArrayObject{
/**
* if identity private variable, in construct you should call parent construct and set the
* instance to this private variable.
* antoher way is never override this construct method
*/
// private $collection;
// function __construct(){
// parent::__construct();
// $this->collection=$this;
// }
function addObject($_index,$_object){
$this->offsetSet($_index,$_object);
}
function getObject($_index){
return $this->offsetGet($_index);
}
function delObject($_index){
$this->offsetUnset($_index);
}
function printCollection(){
echo "<pre>";
print_r($this);
echo "</pre>";
}
}
Store some data
//#get some object
$user=new userObject(); //#general object
$dao=new userDao(); //#DB object
//#put into collection
$colletionInstance=new MySimpleCollection();
$colletionInstance->addObject(1,$user);
$colletionInstance->addObject(2,$dao->dbObjInstance);
$colletionInstance->addObject(3,$_SESSION["URL_FROM"]);
$colletionInstance->addObject(4,serialize("daniel"));
$colletionInstance->addObject(5,unserialize(serialize("daniel")));
$colletionInstance->addObject(6,array(0=>"string",1=>array("name"=>"daniel"),"last name"=>"zhou"));
$colletionInstance->addObject(7,array(0=>"daniel"));
$colletionInstance->addObject(8,0.2356);
$colletionInstance->addObject(9,"daniel");
//#have a look
$colletionInstance->printCollection();
$iterater=$colletionInstance->getIterator();
while ($iterater->valid()) {
echo $iterater->current()."<br>";
echo $iterater->key()."<BR>";
$iterater->next();
}
then take a look
MySimpleCollection Object
(
[1] => user Object
(
[no] => 1
)
[2] => dbObjMySQL Object
(
[db_host] => 192.168.1.166
[db_name] => LOAN163
[user_name] => root
[pass_code] => 2008like18
[db_connect] => Resource id #22
[db_rec] =>
[ErrMsg] =>
[PHPErrCd] =>
[PHPErrMsg] =>
[PHPSQL] =>
[tran_flg] =>
[new_link] =>
)
[3] => /backyard/?Command=Admin_Login
[4] => s:6:"daniel";
[5] => daniel
[6] => Array
(
[0] => string
[1] => Array
(
[name] => daniel
)
[last name] => zhou
)
[7] => Array
(
[0] => daniel
)
[8] => 0.2356
[9] => daniel
)
Object id #6
1
Object id #10
2
/backyard/?Command=Admin_Login
3
s:6:"daniel";
4
daniel
5
Array
6
Array
7
0.2356
8
daniel
9
相关文章推荐
- A very simple MFC class to Encode and Decode an url string
- php a simple smtp class
- java.lang.IllegalArgumentException: object is not an instance of declaring class
- JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method
- Tomcat6环境JBPM4.4报错:java.lang.ClassNotFoundException: de.odysseus.el.util.SimpleResolver
- Android编译错误, Ignoring InnerClasses attribute for an anonymous inner class
- JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method
- AS编译问题:Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
- refers to an unmapped class
- Class.getSimpleName()的作用
- Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
- 反射获取方法时:object is not an instance of declaring class
- Truncated class file description The server encountered aninternal error that prevented it from fulf
- 关于OPENCV The node is neither a map nor an empty collection in function cvGetFileNodeByName
- WebWagon - An HTML Container Class
- Need to specify class name in environment or system property, or as an applet parameter, or in an ap
- Ignoring InnerClasses attribute for an anonymous inner class
- 提示Trying to clone an uncloneable object of class Imagic的解决
- Spring boot 报Add an implement,such as Hibernate Validator, to the classpath
- Use of @OneToMany or @ManyToMany targeting an unmapped class