Tree data structure - can a child access its parent?
I'm using a tree data structure in PHP. The structure is similar to this;
- I have a 'book' object that has properties like 'title' and 'author' and a property called 'chapters'. This is an array of 'chapter' objects.
- Each 'chapter' object has a properties like 'chapter number' and a property called 'pages' - an array of page objects.
- Each page object contains a property called 'text' which is the text to display on the page, and method called as_html that generates the text as an html fragment.
To print out the book, I'd have a method in the book object called 'print' which would do something like;
foreach($this->chapters as $chapter)
foreach($chapter->pages as $page)
So, could anyone tell me whether it is possible for a method in a page object to access the properties of the book to which it belongs? I'm hoping to be able to put something in the as_html method to, for example, insert the title of the book and the author.
Any help gratefully received!