Define and use constructor : Constructor « Class « PHP






Define and use constructor

<?php
   class book {
      private $title;
      private $isbn;
      private $copies;

      public function __construct($isbn) {
         $this->setIsbn($isbn);
         $this->getTitle();
         $this->getNumberCopies();
      }

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

      public function getTitle() {
         $this->title = "title";
         print "Title: " . $this->title . "<br />";
      }

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

   $book = new book("1111");

?>

           
       








Related examples in the same category

1.Instantiate class by calling the constructor
2.Define class as constructor parameter
3.Define Constructor for Class
4.A Class with a Constructor
5.Adding a Constructor to PriceItem
6.Calling the constructor of the parent class
7.Constructors and Destructors
8.Defining an object constructor
9.Creating the Cat constructor
10.Defining object constructors in PHP 4
11.Using the PHP 5 style constructor
12.invoking parent constructors
13.Using Unified Constructors and Destructors
14.Using Default Constructors