SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    Yorkshire, UK
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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;
    Code PHP:
    foreach($this->chapters as $chapter)
    {
      print $chapter->chapter_number;
      {
         foreach($chapter->pages as $page)
         {
           print $page->as_html();
         }
       }
    }

    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!
    There's more to me than meets the eye, but not much more.

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is difficult to say without seeing your classes for Book, Chapter, and Page. It seems like you have three different classes, one for each element of your book, right?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    Yorkshire, UK
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JV- View Post
    It is difficult to say without seeing your classes for Book, Chapter, and Page. It seems like you have three different classes, one for each element of your book, right?
    Yes, that's right. I have classes called Book, Chapter and Page.

    A Book contains an array of several Chapter objects, and each Chapter object could contain several Page objects.

    My view at the moment is that the Chapter class needs a property called parent_book and when the Chapter is instantiated, this property is set to be a reference to the Book object. Likewise, a Page has a property called parent_chapter. Then I might be able to do something like this inside a particular Page object;

    Code PHP:
    echo $this->parent_chapter->parent_book->title;
    to get the book title. Just seems a bit messy.
    There's more to me than meets the eye, but not much more.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doing it that way isn't that big an issue as long as you pass the objects by reference and aren't cloning them


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •