PHP Objects (Part 2)

Hope you have created some classes of your own. To get the gist let create another class Automobile

<?php
 class Automobile {
       public $brand;
       public $color;
       public $wheels;
       public $engine_type;
 }
?>

This class is now a simple blueprint for your car. But where is the car?

PHP Objects

As per definition in object oriented programming paradigm, an object is an instance of a class. So if Automobile was a class then a Porsche Cayenne is your object. Lets elaborate.

If you’re reading this, chances are that you’re a male or a female of the human species. Your properties are your eyes, ears, nose, hands, legs etc. You belong to the Human species. You might argue that other animals have these properties as well. True. But for the sake of this chapter, lets stick to our species.

So you’re a male who belong to the Human species or a female who belongs to the same. In object oriented paradigm, the male and female are the objects of the class Humans. Your male/female friends are members of the same class. But your characteristics and behaviors are different from those of your friends. As same as with PHP objects. In other words a class can have n number of objects. As of 2016, class Human has more that 7 billion objects (pun).

Lets return back to our Book class. Lets declare our properties.

<?php

class Book { // Declare PHP Class Book

  public $isbn;
  public $book_title;
  public $book_author = array(); // often there are multiple authors
  public $book_category;
  public $book_publish_date;
  public $book_price;
  public $book_language = array(); // multiple languages
  public $book_format = array(); // multiple formats
  
}

To create PHP objects we use the ‘new’ keyword followed by the class name. Let’s populate the object data one by one.

<?php 
  $book_one = new Book; // Declare Object
  
  // Lets take our Oliver Twist example
  
  $book_one->isbn = "123-456-890";
  $book_one->book_title = "Oliver Twist";
  $book_one->book_author = "Charles Dickens";
  $book_one->book_category = "Literature";
  $book_one->book_publish_date = "June 2001";
  $book_one->book_price = "$7";
  $book_one->book_language = array('English', 'Spanish');
  $book_one->book_format = array('Paperback', 'Hardcover', 'Kindle');
  
  
  // bit modification of properties with array values
  
  $book_one->book_language = implode(", ", $book_one->book_language);
  $book_one->book_format = implode(", ", $book_one->book_format);

?>

Storing a variable data and object data is slightly different. Since an object is a collection of class properties and methods (which we will study later), it needs to identify its properties and methods properly. The object operator (->) is used for accessing the object’s own properties or methods. Think of your eyes as an example. You use your own eyes to view the world not someone else’s, just like every human has his own set of eyes. Same is with PHP objects.

Now that we have entered our data, let’s display all the book object information in an HTML tabular fashion. On the same file after closing the PHP delimiter, write this code. Its a basic HTML table with few basic CSS styles.

<!DOCTYPE html>
<html>
  <head>
    <title>Books</title>
    <style>
      table {
        border: 1px solid #ddd;
      }

      td, 
      th {
        font-family: Arial;
        font-size: 1.2em;
        padding: 1em;
      }

      thead tr th {
        background: #ddd;
        text-align: center;
      }
     </style>
  </head>

  <body>
    <table>
      <thead>
         <tr>
           <th>ISBN</th>
           <th>Book Title</th>
           <th>Book Author</th>
           <th>Book Category</th>
           <th>Publish Date</th>
           <th>Price</th>
           <th>Languages Available</th>
           <th>Formats Available</th>
         </tr>
     </thead>
    <tbody>
        <tr>
          <td><?php print $book_one->isbn; ?></td>
          <td><?php print $book_one->book_title; ?></td>
          <td><?php print $book_one->book_author; ?></td>
          <td><?php print $book_one->book_category; ?></td>
          <td><?php print $book_one->book_publish_date; ?></td>
          <td><?php print $book_one->book_price; ?></td>
          <td><?php print $book_one->book_language; ?></td>
          <td><?php print $book_one->book_format; ?></td>
        </tr>
     </tbody>
  </table>
 </body>
</html>

You will get this result in your browser

Book Object Result

Lets add another book to our Book class.

<?php
  $book_two = new Book; // Declare Object
  
  // Lets take our Oliver Twist example
  
  $book_two->isbn = "890-765-321";
  $book_two->book_title = "Cosmos";
  $book_two->book_author = "Carl Sagan";
  $book_two->book_category = "Science";
  $book_two->book_publish_date = "June 1991";
  $book_two->book_price = "$15";
  $book_two->book_language = array('English', 'Spanish', 'German');
  $book_two->book_format = array('Paperback', 'Kindle');
  
  $book_two->book_language = implode(", ", $book_two->book_language);
  $book_two->book_format = implode(", ", $book_two->book_format);
?>

Add another table row and you get this result

Book object result

This kind of information is shown on e-commerce sites like Amazon, where data is converted to meaningful information for the user.
You can add n number of books to this class, but what do we do with these data? The answer lies in class “methods” which we will study next.

Meanwhile, create some more classes and their respective objects and play around.

Recommended resource: Class basics PHP.net

Leave a Comment

Your email address will not be published. Required fields are marked *