|
Posted by Hilarion on 09/06/05 12:26
> In a content administration tool I call classes from inside classes in order
> to separate the admin functions from the display-only functions:
>
> class book
> {
> var $title;
> var $author;
> var $adminfunctions;
>
> function book($admin = false)
> {
> if ($admin) {
> $this->adminfunctions =& new book_admin($this);
> }
> }
>
> function display_book()
> {
> return $this->author.": ".$this->title;
> }
> }
>
> class book_admin
> {
> var $boss;
>
> function book_admin(&$boss)
> {
> $this->boss = $boss;
As Chung Leong pointed out you should probably change the line
above to
$this->boss =& $boss;
> }
>
> function update_book($author, $title)
> {
> $this->boss->author = $author;
> $this->boss->title = $title;
> }
>
> [lots of other methods]
> }
>
> The admin code will be something like:
>
> $book =& new book(true);
> $book->adminfunctions->update_book("Michael Crichton", "Jurassic Parc");
> echo $book->display_book();
>
> As you see there is a circle reference, as book_admin::boss is a reference
> back to book. I assume this is bad, and actually there are some problems
> that I suspect are originated there.
Circular references are not something bad in general. Reference to "parent"
in a "child" and to "children" from "parent" are usually ok.
> Is there another possibility to access properties of the calling object? I
> did not find any syntax similar to the parent::property syntax, and I can't
> use inheritance, as there is already a vertical inheritance (such as page
> extends book extends library...), and it is not possible to inherit two
> classes.
You are using some strange inheritance (as you described it, but it's not
in your example code). Book should not extend page, page should not extend
book. Book should contain pages (have references to them), page should (or
could) have reference to the book. Same with library and books. In your
scenario each page can be treated as a book or a library and each book can
be treated as a library. You should probably also not use inheritance
between book and book admin.
Hilarion
[Back to original message]
|