PHP - Adding Class properties

Introduction

The following code adds the properties of books:

  • title
  • author
  • ISBN
  • available or unavailable.

Demo

<?php 

     class Book { 
        public $isbn; 
        public $title; 
        public $author; 
        public $available; 
     } /*  w ww . j  a va2s. c om*/

     $book = new Book(); 
     $book->title = "2018"; 
     $book->author = "C"; 
     $book->available = true; 
     var_dump($book); 

     $book1 = new Book(); 
     $book1->title = "2018"; 
     $book2 = new Book(); 
     $book2->title = "C++"; 
     var_dump($book1, $book2); 

?>

Result

When creating multiple instances of an object and assigning values to their properties, each object will have their own values

Related Topics