RE: [php-objects] collections

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> So what if I want to remove object 3 in a collection of 5?  I can build a
delete method into the class, that will redim the collection array?

[Very slow response]

I do this pretty often in an app I'm working on right now. One object has an
array of other objects. Generally the classes for those objects have their
own methods for things like deleting. I also have a &get_object() method
that returns a specific instance in the array.

In most case the array keys off the key field in the database - which are
NOT sequential - so it's not as simple as objects[3]->delete(). In the worst
case you can use a for/foreach loop to find the specific object to operate
on, then break out.

Foo has a var called bars that is an array of bar objects. It also has a a
method &get_bar() which returns a specific instance of a bar object.

So if I want to delete a specific bar object, I pass a param for the bar
object's ID.

Function delete_bar($ID) {
	$bars=$this->bars;
	foreach(bars as $key=>$value) {
		if(bars[$key]->get_ID()==$ID {
			bars[$key]->delete();
			unset($this->bars[$key]);
			break;
		}
	}
}

...or if the array is keyed off $ID (my preferred approach)

Function delete_bar($ID) {
	return $this->bars[$ID]->delete();
}

...or you can do something like

$bar=$foo->get_bar($ID);  //returns a reference
if($bar) {
	$bar->delete();
}


Not recommended if you potentially have hundreds or thousands of bar objects
in the bars array, but reasonably fast if you're only managing a few.

foreach() operates on a copy of the array, not the array itself, so it could
cause confusion in some cases, but it hasn't been a problem for my specific
needs.

-Derek



------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/LIgTpC/vN2EAA/xGHJAA/saFolB/TM
---------------------------------------------------------------------~->

Look here for Free PHP Classes of objects:
http://phpclasses.UpperDesign.com/
To unsubscribe from this group, send an email to:
php-objects-unsubscribe@egroups.com

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux