Accessing other Same-Level Objects
Posted: Thu May 12, 2005 6:09 pm
The title's a bit obtuse, so I'll try to explain.
Let's consider a database system where we have an author: this author is represented by the class TWP_Data_User_Author. Inside, we have an array of TWP_Data_Story objects (which each have TWP_Data_Chapter objects in them). The Author object contains information about the author, such as Real Name, email address, etc.
Let us consider we have another type of class: TWP_Data_User_Project. This is a project that people can participate in: it's sorta like an author, but has many differences.
Our TWP_Data_User_Author "bob" has participated in TWP_Data_User_Project "wikipedia". His author page will display some relevant information about TWP_Data_User_Project.
The question, now, is where we put the TWP_Data_User_Project.
Currently, the main object is TWP_Data_User_Author "bob", and that's the basis of the page display. In order to grab information about TWP_Data_User_Project while using methods inside TWP_Data_User_Author, we could store the project in a variable in the object, like: $this->project_object
However, there are also project pages, which display information about the authors who have participated in this project. If we were to follow similar logic, there would be a $this->author_object variable. As you can see, this could lead to infinite recursion.
When you use an object, it may need to access the information of another object: but when this object is not clearly within it's hierarchy, trying to put it as a lower-level object will cause organization problems. This means:
It doesn't make sense, and it the fix is ugly (probably involves passing a variable stating how many times you should recurse through the object)
But does this mean we create a global object that contains all the Author and Project objects? And if that's the case, how would an Author object access information from a Project by using one of it's own methods?
Obviously, $this->project won't work. Is it possible using only Author's methods? Does this mean interobject methods have to be stored independent of the Author and Project objects?
I'm a bit confused, but this seems like a kind of question that has been addressed in past programs. What is the correct design for this sort of situation?
Let's consider a database system where we have an author: this author is represented by the class TWP_Data_User_Author. Inside, we have an array of TWP_Data_Story objects (which each have TWP_Data_Chapter objects in them). The Author object contains information about the author, such as Real Name, email address, etc.
Let us consider we have another type of class: TWP_Data_User_Project. This is a project that people can participate in: it's sorta like an author, but has many differences.
Our TWP_Data_User_Author "bob" has participated in TWP_Data_User_Project "wikipedia". His author page will display some relevant information about TWP_Data_User_Project.
The question, now, is where we put the TWP_Data_User_Project.
Currently, the main object is TWP_Data_User_Author "bob", and that's the basis of the page display. In order to grab information about TWP_Data_User_Project while using methods inside TWP_Data_User_Author, we could store the project in a variable in the object, like: $this->project_object
However, there are also project pages, which display information about the authors who have participated in this project. If we were to follow similar logic, there would be a $this->author_object variable. As you can see, this could lead to infinite recursion.
When you use an object, it may need to access the information of another object: but when this object is not clearly within it's hierarchy, trying to put it as a lower-level object will cause organization problems. This means:
Code: Select all
Rule: Author contains Project
Rule: Project contains Author
Author contains {Project contains {Author contains ...}}But does this mean we create a global object that contains all the Author and Project objects? And if that's the case, how would an Author object access information from a Project by using one of it's own methods?
Obviously, $this->project won't work. Is it possible using only Author's methods? Does this mean interobject methods have to be stored independent of the Author and Project objects?
I'm a bit confused, but this seems like a kind of question that has been addressed in past programs. What is the correct design for this sort of situation?