Re: Listing objects





Lüpher Cypher wrote:

Hi,

Suppose we have a hierarchical class structure that looks something like this:

Object
  |
  +-- Main
  |
  +-- Object1
  |     |
  |     +-- Object11
  |     |
  |     +-- Object 12
  |
  +-- Object2
  |
  +-- Object3
  |
  +-- Object4
        |
        +-- Object41
        |     |
        |     +-- Object 411
        |
        +-- Object 42

Now, the application looks like this:

<?php
$app = new Main(...);
$app->doSomething();
?>

Class Main() is instantiated only once, however it works with all other classes, which may be instantiated a number of times.

Now suppose there is a scenario:

Main creates Object11.
Object11 calls Object1::foo(), which creates Object2.
Object2 may then create Object1, in which case
  Object1 calls its foo(), which creates Object2.
  Object2 may then create Object1
     ..etc. until Object2 decides not to create Object1..

So, basically we'd get:

Main
  Object11->foo()
    Object2
       Object1->foo()
         Object2
           ...

Now, when Object2 instantiates, it assigns a value to its variable, say, Object2::var, which should be unique. So, after assigning the value, Object2 has to check it against any other Object2::var.
However, Object2 is not necessarily instantiated under Object1, so, let's say we have


Main
  $obj11 = Object11
    $obj2 = Object2
       $obj1 = Object1
         $obj2 = Object2
  $obj3 = Object3
    $obj2 = Object2
  $obj411 = Object411
    $obj42 = Object42
      $obj1 = Object1
        $obj2 = Object2


Assume $app->obj11->obj2->obj1->obj2::var should be checked against

$app->obj11->obj2::var
$app->obj3->obj2::var
$app->obj411->obj42->obj1->obj2::var

I assumed the easiest way would be to have a global array of all objects descending from Object and update it in Object's constructor:

function Object() {
  global $objects;
  $objects[count($objects)] = &$this;
  ...
}

Then we could simply iterate through $objects, and if object's class is Object2, compare the var's.
However, this does not work, even through the object is passed by reference - if the object's variables are updated (by the object itself or by another object), the global array apparently has a copy of the initial object state:


$obj2 = new Object2();
  results in
$obj2 = Object2( var => 1 )
$objects[0] = Object2( var => 1 )

$obj2->var++;
  results in
$obj2 = Object2( var => 2 )
$objects[0] = Object2( var => 1 )

So, I guess, some other mechanism should be used.


Any ideas?

Thanks,
luph

This is a textbook case of a badly designed set of classes. Their relationships are so complicated that even you the architect, are having problems explaining yourself. I think you'll need to go back to the drawing board on this one.


Extract the common features into a set of interfaces which should help clarify the class relationships and clean up the design. Also wherever possible, try to use (design) patterns, that way, your code will be reusable (not to mention easir to maintain). Incidentally, your Main class looks like a Singleton pattern, but only you the archiect, can know that for sure since your description given here is not very lucid.

hope that helps

.