doni49 Posted October 14, 2006 Share Posted October 14, 2006 I've been trying to get into using classes and objects. I think I understand them fairly well and decided that I'd see if I could come up with a useful generic use class. As I look at the power of classes and OOP, I can see the need for using lots of arrays of objects (a car dealership might use a class to define the cars in their inventory and they'd want to have an array of "Car" objects for example).So I decided to test myself. I created a "Collection" class that includes methods to add, insert and remove items to and from the array. I did this thinking that it would be a major PITA to have to write the code to do them every time. Plus it was a great learning experience!I'd like to get the thoughts of those of you who've been using OOP/PHP for a while now and see if you can make any suggestions as to how this can be improved. As well as are there any other properties/methods you think I should add? A usage example follows below.Anyone care to critique it? Thanks! I've tested it in PHP 5.x (not sure of the sub-version).[code]class collection{ var $count=0; var $items = array(); private $type; function collection($type){ $this->type = $type; } private function buildObject($args, $index, $isIndex){ $newItem = "new " . $this->type ."("; if($isIndex AND count($args) > 0){ $args = array_slice($args,1); } //echo "<br />Index 2: " . $index; if(count($args)>1){ $k = ""; $i = 1; foreach ($args as $a){ $k .= "'" . $a . "'"; if($i < count($args)){ $k .= ", "; } $i += 1; } $newItem = $newItem . $k; }elseif(count($args)== 1){ $newItem = $newItem . $args[0]; } if ($index < 0){$index = $this->count;} $newItem .= ");"; eval('$this->items[' . $index . '] = ' . $newItem); } function removeItem($index){ if ($index < 0){ die ('"Remove" Index must be positive.'); }elseif($index >= $this->count){ die('"Remove" Index must be less than the number of items.'); }else{ if ($index==0){ $this->items = array_slice($this->items, 1); }elseif (count($this->items) == $index){ $this->items = array_slice($this->items, 0, $index-1); }else{ $front = array_slice($this->items,0,$index - 1); $back = array_slice($this->items, $index + 1); $this->items = array_merge($front, $back); } $this->count = $this->count - 1; } } //add an item to the end of the "items" array. //all arguments are passed to the item's constructor if any. //all items are optional function addItem(){ $args = func_get_args(); if(count($args) > 0){ $this->buildObject($args,$this->count, FALSE); }else{ $this->buildObject(FALSE,$this->count, FALSE); } $this->count += 1; } //insert an item into the "items" array at the location specified by "index". //Index MUST be the first argument to the function. All remaining arguments are //optional and will be passed to the item's constructor if any. function insertItem(){ $args = func_get_args(); $index = $args[0]; array_shift($args); if ($index < 0){ die ('"Insert" Index must be positive.'); }else{ if ($index < $this->count){ if($index > 0){ //if index is greater than zero but less than the //number of items in the "items" array, then //seperate items into two arrays and then rebuild //"items" array. $front = array_slice($this->items, 0, $index); $back = array_slice($this->items, $index); $this->items = array_merge($front,array(""),$back); }else{ //if index is equal to zero, then add space for the new item. $this->items = array_merge(array(""),$this->items); $index = 0; } }else{ //if index is equal to the number of items in the "items" array, //then put this new item at the end of the array. $index = $this->count; } $args = func_get_args(); if(count($args)> 0){ $this->buildObject($args, $index, TRUE); }else{ $this->buildObject(FALSE, $index, TRUE); } $this->count += 1; } }}[/code]Usage Example:[code]class car{ var model; var make; var color; function car($make, $model, $color){ $this->model=$model; $this->make=$make; $this->color=$color; }}$cars = new collection("car");$cars->add("Chevrolet", "Blazer", "Green");$cars->add("Ford","Mustang","Blue");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23921-classes-objects-collections/ Share on other sites More sharing options...
doni49 Posted October 17, 2006 Author Share Posted October 17, 2006 ANYONE out there have any thoughts for improvements? Quote Link to comment https://forums.phpfreaks.com/topic/23921-classes-objects-collections/#findComment-110189 Share on other sites More sharing options...
Barand Posted October 17, 2006 Share Posted October 17, 2006 Sort on make, model or color? And a way a user can get the collection returned once they have added to it.$cars->sort('color', 'DESC');$obj_array = $cars->get_items();or $item = $cars->next_item(); Quote Link to comment https://forums.phpfreaks.com/topic/23921-classes-objects-collections/#findComment-110292 Share on other sites More sharing options...
doni49 Posted October 17, 2006 Author Share Posted October 17, 2006 I like the idea of next/prev & sort sounds do-able too. But I'm not sure of the need for "get_items". It seems to me that $obj_array = $cars->get_items() would do the same as $obj_array = $cars->items.Thanks for the input, anyone or anything else?This has been a great learning experience for me as I think I have a decent grasp on classes and objects as a result of this. Eventually if this gets to be good enough, I figured that I'd submit it for the code library here. But I think that's a bit down the road. Quote Link to comment https://forums.phpfreaks.com/topic/23921-classes-objects-collections/#findComment-110324 Share on other sites More sharing options...
doni49 Posted October 23, 2006 Author Share Posted October 23, 2006 OK. I think I've got it. I added the following methods: Next Previous Current IndexOf sort sortrI tried to post the code here, but the page just kept timing out. I'm guessing there's a limit to the message size. I think it could prove useful for more than just me so I'm gonna submit it for inclusion in the code library. Does anyone know how to do that? I thought there'd be some kind of "Submit Code" link in the code library area but I didn't see one. Quote Link to comment https://forums.phpfreaks.com/topic/23921-classes-objects-collections/#findComment-113269 Share on other sites More sharing options...
9911782 Posted October 24, 2006 Share Posted October 24, 2006 Hi AllI have started using PHP & MYSQL this year. I have done 2 application with the knowledge I get from the forums and trainings.I booked myself to the PHP Advanced course, they were training us on using the classes too. Can somebody help me to understand the classes more. I need to use the classes in my applications. Can I get the site where I could get the tutorial to learn more on how to create classes.Thank you :)9911782 Quote Link to comment https://forums.phpfreaks.com/topic/23921-classes-objects-collections/#findComment-113523 Share on other sites More sharing options...
doni49 Posted October 24, 2006 Author Share Posted October 24, 2006 Please start a new thread for this new topic. Quote Link to comment https://forums.phpfreaks.com/topic/23921-classes-objects-collections/#findComment-113595 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.