Jump to content


Photo

Classes, Objects, Collections


  • Please log in to reply
6 replies to this topic

#1 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 14 October 2006 - 05:38 AM

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).

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;
		}
	}
}

Usage Example:
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");

Don

#2 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 17 October 2006 - 06:35 PM

ANYONE out there have any thoughts for improvements?
Don

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 17 October 2006 - 10:10 PM

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();
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 17 October 2006 - 10:56 PM

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.
Don

#5 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 23 October 2006 - 05:14 PM

OK.  I think I've got it.  I added the following methods:
    Next
    Previous
    Current
    IndexOf
    sort
    sortr

I 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.
Don

#6 9911782

9911782
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 October 2006 - 09:04 AM

Hi All
I 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

#7 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 24 October 2006 - 12:11 PM

Please start a new thread for this new topic.
Don




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users