Page 1 of 1

Objects within Objects

Posted: Wed Nov 06, 2002 7:31 am
by TheBlackBadger
Hi all,
First off I'm a bit of a newbie with PHP so my terminology might be a bit off, but I've got some Lasso 6 experience.

OK my brickwall of a problem is:
I've got an instance of an object (M) which has a property in it that is an array, in this array a number of other instances of differing types of classes are stored. Each of these objects also has a property that is an array, which also stores more objects.... and so on, building up a hierarchy.
So how would I go about saying "OK, find me all the objects that are of type xyz and property abc is blue and put them in an array called Things" ?

The only way I can think of doing this is by using some form of recursion to go through each object and examine it's array, if it passes insert into Things.

Any advice in laymans terms of how I should go about this?

Hope this is clear.

Many thanks.

Posted: Wed Nov 06, 2002 7:53 am
by f1nutter
Sounds like a tree to me, perfect for recursion.

In no particular language:

Code: Select all

class tree_node
{
 string name;
 array children;
}

tree_node root = new tree_node;
tree_node leaf =  new tree_node;

root -> childrenї0] = leaf;
This builds your tree, and everything hangs from the root.

Code: Select all

function test_tree(root)
{
 if (root == NULL)
 {
  return;
 }
 else
 {
  // do some tests / assignments
  for each child
  {
   test_tree(root -> childrenї0]);
  }
 }
}
???