Re: Listing objects
- From: Dikkie Dik <nospam@xxxxxxxxxx>
- Date: Fri, 30 Dec 2005 17:04:34 +0100
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..
Sorry, I'm probably confusing with other languages.
Ok, let me try a clearer version :)
Object | +--- App | ::$page | +--- Control | ::$parent | ::$data | ::$object | +--- Data | ::$id | +--- Template | ::owner | ::control | +--- Page
This is the inheritance hiearchy, I assume that you also have an association hierachy. App will be the root, and then...
I would think that an App creates a page and creates a data channel, or asks a Control to do that. Within my perception, a page has a template and date (gets it from App). So App knows Control and Page, and also knows that Control has Data. All these objects do not have to know App as far as I can see.
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 the problem is here, let's keep it here. I have written my own DatabaseID class with its own IsEqualTo method (and an IsNew method). For the very simple reason: A data-object might be constructed with database data (known ID) or be a new record that was not yet stored in the database. With this DatabaseID class, I can give EVERY data-object an ID. If the DatabaseID instance is not contructed with a database ID, the IsEqualTo method will always return FALSE. Two new records are per definition different in my application. Only if both compared objects were constructed with an ID from the database, an actual by-value comparison if done.
...
if ($some_condition) {
$object = new $CustomSubclassOfTemplate($this->parent);
}
}
What is template doing in Control? If Control is responsible for both templates and data, it is a clear signal that this class wants to be split into two classes.
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)
Is that bad? Frankly I don't care how deep a structure goes, as long as it makes sense and is finite.
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.
Yikes!
One of the main features of object-oriented programming is that you can nicely shield off what others should not access. The contents of a page should only be accessible by asking the $page object, which is only accessible by asking $app. Why? Because it allows $page to decide how and when to fill in the data, and to throw an exception if no data was passed. That is $page's reponsibility, not $app's, and not even yours. If you want to peek inside of $page's internals, write a temporary method or echo statement, during development only.
Not even your responsibility? No. You can program it, write a unit test for it, and when it is finished, it can live on its own. It is sad, but the only thing left for you to do is typing the URL...
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
.
- Follow-Ups:
- Re: Listing objects
- From: Lüpher Cypher
- Re: Listing objects
- References:
- Listing objects
- From: Lüpher Cypher
- Re: Listing objects
- From: Dikkie Dik
- Re: Listing objects
- From: Lüpher Cypher
- Listing objects
- Prev by Date: Re: _COOKIE[] unique?
- Next by Date: Re: Cannot get this one correct and working, though simple!
- Previous by thread: Re: Listing objects
- Next by thread: Re: Listing objects
- Index(es):