Skip to main content

A PHP example class.

<?php

class Book {
  
  // -----------------------------
  // Private Members
  // -----------------------------
  private $title;
  private $isbn;
  private $copies;

  /**
   * Sample PHP Class
   * 
   * @param string $isbn
   */
  public function __construct($isbn) {
    $this->setIsbn($isbn);
    $this->getTitle();
    $this->getNumberCopies();
  }
  
  /**
   * Destructor
   */
  function __destruct() {
    echo "<p>Book class instance destroyed.</p>";
  }

  public function setIsbn($isbn) {
    $this->isbn = $isbn;
  }

  public function getTitle() {
    $this->title = "Easy PHP Websites with the Zend Framework";
    print "Title: " . $this->title . "<br />";
  }

  public function getNumberCopies() {
    $this->copies = "5";
    print "Number copies available: " . $this->copies . "<br />";
  }
}

// Instantiate a Book Object
$book = new book("0615303889");

?>