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>";
}
}
* @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();
}
$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
(
[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
本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/131334,如需转载请自行联系原作者