Re: Listing objects



Dikkie Dik wrote:
As Peter Fox has pointed out, put the tasks where they belong. I have the feeling that you are playing the role of a conductor who has to learn each musician how to play as well.

Use clear names for objects. If your model represents companies, departments, employees, tasks and subtasks, just call your classes those descriptive names.

I figured that much :)


If you have to compare different instances that might have the same state (value of internal variables), you could give the class an IsEqualTo method to do the comparison. Even better, a lazy collection can be used to ensure that equal objects are actually the same instance. You could then just use the reference equality operator (===).

Hmm.. $a === $b will return true if $a == $b (i.e. their properties have the same values) and they are of the same class. That doesn't mean they are the same object, though..



Ok, let me try a clearer version :)

Object
  |
  +--- App
  |      ::$page
  |
  +--- Control
  |      ::$parent
  |      ::$data
  |      ::$object
  |
  +--- Data
  |      ::$id
  |
  +--- Template
         |  ::owner
         |  ::control
         |
         +--- Page

In the main script I have

$app = new App();

In constructors I have

function App() {
   ...
   $null = null;
   $this->page = new Page($null);
}

function Page(&$owner) {
   parent::Template(&$owner);
}

function Template(&$owner) {
   $this->owner = &$owner;
   ...
   $this->control = new Control(&$this);
}

function Control(&$parent) {
   $this->parent = &$parent;
   ...
   $this->data = new Data();
   ...
   if (already_exists($this->data->id)) {
      echo "Duplicate ID";
      return;
   }
   ...
   if ($some_condition) {
      $object = new $CustomSubclassOfTemplate($this->parent);
   }
}

function Data() {
   ...
   $this->id = $some_retrieved_id;
}

Basically, the interesting part is:

When Template is instantiated (initially Page in App constructor), it instantiates Control, which, in turn, instantiates Data. At this point, $data->id will be set.
At the end of Control's constructor, if some condition is satisfied, a subclass of Template will be instantiated and stored in Control's $object.
Thus, we can have:
$app->$page->$control->$object->$control->$object->...->$control->null($object)
We can also pretty much go bottom-up through Template objects via $owner and we can get the Template object from Control via $parent.


Now, in the middle of Control's constructor there is a check that $data-id does not already exist.

One way would be to go through Templates:

$temp = $this->parent;
while (isset($temp)) {
  if ($this->data->id == $temp->control->data->id) {
     echo "error";
     return;
  }
}

Apparently this does not work, even though owner is passed by reference.

Another way would be to have a global array and save all objects there by reference. This is actually more appealing to me, since then I could access any object at any time from anywhere. Say, if I have an array of objects under Template and each of them has an associative array key=>value, then to "compile" an associative array from all those arrays, I'd simply iterate through the global array, check object's class, and take in the key/value pairs in the resulting array, rather than traverse a tree of Templates and those objects. Anyways, what I tried is something like this:

global $objects;
$objects = array();

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

However, if a property of an object is changed, the object in $objects stays the same.. But then, it must be a copy?


luph .


Quantcast