An Simple Collection Class

简介:
 
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 

Object id #10 

/backyard/?Command=Admin_Login 

s:6: "daniel"

daniel 

Array 

Array 

0.2356 

daniel 
9


    本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/131334,如需转载请自行联系原作者



相关文章
|
18天前
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
53 18
你对Collection中Set、List、Map理解?
### Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have
### Cause: java.sql.SQLException: Field ‘id‘ doesn‘t have a default value; Field ‘id‘ doesn‘t have
|
7月前
Cannot load keys from store: class path resource
Cannot load keys from store: class path resource
393 0
|
Java 关系型数据库 MySQL
15. 成功解决:java: Can't generate mapping method with primitive return type.
今天启动 SpringBoot 项目时,报了如下错误:`java: Can't generate mapping method with primitive return type.`
1054 0
Could not resolve element type of Iterable type xxxxx.RequestParam java.util.List<?>. Not declared?
Could not resolve element type of Iterable type xxxxx.RequestParam java.util.List<?>. Not declared?
Could not resolve element type of Iterable type xxxxx.RequestParam java.util.List<?>. Not declared?
【Junit 报错】Test class should have exactly one public zero-argument constructor和Test class can only have one constructor
错误1: 1 java.lang.Exception: Test class should have exactly one public zero-argument constructor 2 at org.
6170 0
|
存储 索引 容器
List、Set、Map是否继承自Collection接口?
List、Set 是,Map 不是。Map是键值对映射容器,与List和Set有明显的区别,而Set存储的零散的元素且不允许有重复元素(数学中的集合也是如此),List是线性结构的容器,适用于按数值索引访问元素的情形。
1035 0
|
存储 Java
Java Primitive type与References type
Java语言规格文档与JVM规格文档不一致... surprice Java是一种强类型语言,它语言规范中定义了Java的数据类型分为,基本数据类型和引用数据类型。