You are here: Re: php nesting classes « PHP Programming Language « IT news, forums, messages
Re: php nesting classes

Posted by Toby A Inkster on 04/23/07 11:44

Jerry Stuckle wrote:

> Either an order has tickets, or a ticket has orders. But can you
> explain how both are true?

I often do this sort of thing. Consider a list of authors and a list of
books that they've written:

<?php
class Author
{
private $surname = '';
private $forename = '';
private $books = array();

function __construct ($surname, $forename='')
{
$this->surname = $surname;
$this->forename = $forename;

if (!strlen($this->surname))
throw new Exception("Authors need a surname!");
}

function get_name ($reverse=FALSE)
{
if (!strlen($this->forename))
return $this->surname;

if ($reverse)
return "{$this->surname}, {$this->forename}";
else
return "{$this->forename} {$this->surname}";
}

function print_catalogue ()
{
print $this->get_name() . "\n";
foreach ($this->books as $b)
print "\t".$b->title."\n";
}

function give_credit (Book $book)
{
$book->author = $this;
$this->books[] = $book;
}
}

class Book
{
public $title;
public $author = NULL;

function __construct ($title)
{
$this->title = $title;

if (!strlen($this->title))
throw new Exception("Books need a title!");
}

function get_reference ()
{
if ($this->author instanceof Author)
return $this->author->get_name().': '.$this->title;
else
return $this->title;
}
}


$library['emma'] = new Book('Emma');
$library['pp'] = new Book('Pride & Prejudice');
$library['ss'] = new Book('Sense & Sensibility');
$library['wh'] = new Book('Wuthering Heights');
$library['dict'] = new Book('Concise Oxford English Dictionary');

$authors['JA'] =new Author('Austen', 'Jane');
$authors['JA']->give_credit($library['emma']);
$authors['JA']->give_credit($library['pp']);
$authors['JA']->give_credit($library['ss']);
$authors['JA']->give_credit(new Book('Love & Freindship'));

$authors['EB'] =new Author('Bronte', 'Emily');
$authors['EB']->give_credit($library['wh']);

print "My library of classic literature...\n\n";
foreach ($library as $book)
print $book->get_reference()."\n";
print "\n";
print "All known works of classic literature...\n\n";
foreach ($authors as $author)
$author->print_catalogue();
print "\n";
?>

Bi-directional links between objects are quite common. All authors have
a list of books; all books have an author. In a more advanced
implementation, there might be a Library class too, and it might be a good
idea to take into account the fact that some books have more than one
author.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация