Objects within Objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheBlackBadger
Forum Newbie
Posts: 1
Joined: Wed Nov 06, 2002 7:31 am

Objects within Objects

Post 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.
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post 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]);
  }
 }
}
???
Post Reply