A Simplified PHP Class

A class is a type, often illustrated in UML by a class diagram as follows. Once you have a class you define instances of the class.

Figure 1.1. A Class Diagram of a Book Class

Example 1.2. The Class
<?php
/**
 * Description of Book
 *
 * @author nml
 * @version 
 */
class Book {
    private $title;
    private $author;
    private $edition;
    private $publisher;
    private $year;
    
    public function __construct($title, $author, $ed, $pub, $year) {
        $this->title = $title;
        $this->author = $author;
        $this->edition = $ed;
        $this->publisher = $pub;
        $this->year = $year;
    }
    
    public function getTitle() {
        return $this->title;
    }
    public function setTitle($title) {
        $this->title = $title;
    }
    public function getPublisher() {
        return $this->publisher;
    }
    public function __toString() {
        $s = "";
        $s .= sprintf("%s, <em>%s</em>, %s, %s\n", 
                $this->author, $this->getTitle(), $this->getPublisher(), $this->year);
        return $s;
    }
}

Example 1.3. The User of the Class
<!DOCTYPE html>
<?php
    require_once 'Book.inc.php';
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
<?php
        $book = new Book('Canvas Pocket Reference',
                         'David Flanagan',
                         '1st ed',
                         'O&apos;Reilly',
                         '2011');
        $greeting = 'Here is a book:';
        printf("<h1>%s</h1>\n", $greeting);
        printf("<h2>Books from %s</h2>\n", $book->getPublisher());
        printf("<br/>%s\n", $book);
?>
    </body>
</html>

See the page in your browser.