PHP collection class

Source: Internet
Author: User
<? PHP  /*  ** PHP collection class (1)  */  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' => 'inc', 'code' => 88 )) , 'Xiaoxin' => Array  (  Array ('Id' => 3, 'name' => 'Math', 'code' => 77 ), Array ('Id' => 4, 'name' => 'inc', '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 ();} ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.