Manjit Baishya
My Coding Roadmap

My Coding Roadmap

Day 20: CLASS statement in PHP

Day 20: CLASS statement in PHP

Manjit Baishya's photo
Manjit Baishya
·Sep 12, 2022·

3 min read

There are a limited number of data types present in PHP, such as string, integer, float and so much more. Now, a situation may arise that we will be in need of another data type. Now, this is a situation where we require a special PHP element called class. A class is used to define a special type of variable. An example will clear all things up.

Suppose we are required to generate a special data type for the information of a particular book. We can proceed as follows:

<?php

    //defining anew class
    class book
    {
      //defining new variables
      var $title;
      var $author;
      var $pages;
    }

    //assining a variable to the above created class
    $book1 = new book();

    //assining values to the class above
    $book1->title = "Harry Potter";
    $book1->author = "J.K. Rowlin";
    $book1->pages = 400;

    //printing required variable
    echo $book1->author;

 ?>

Output>>>

J. K. Rowlin

Here, we have created a class namely book inside which, we have defined the author, title and the total pages. We then create a new variable to hold the value of that new class and later print out the required data. The book used later after new is called an object.

Similarly, we do this for a large number of objects:

<?php

    class book
    {
      var $title;
      var $author;
      var $pages;
    }

    //declaring object 1
    $book1 = new book();
    //declaring properties of the object
    $book1->title = "Harry Potter";
    $book1->author = "J.K. Rowlin";
    $book1->pages = 400;

    //printing data
    echo "The author of $book1->title is $book1->author and it is $book1->pages pages. <br>";

    //declaring object 2
    $book2 = new book();
    //declaring properties of the object
    $book2->title = "Anna Sewell";
    $book2->author = "Black Beauty";
    $book2->pages = 700;

    //printing data
    echo "The author of $book2->title is $book2->author and it is $book2->pages pages. <br>";

    //declaring object 3
    $book3 = new book();
    //declaring properties of the object
    $book3->title = "Relativity";
    $book3->author = "Albert Einstein";
    $book3->pages = 1200;

    //printing data
    echo "The author of $book3->title is $book3->author and it is $book3->pages pages. <br>";

 ?>

Output>>>

The author of Harry Potter is J.K. Rowlin and it is 400 pages.
The author of Anna Sewell is Black Ceauty and it is 700 pages.
The author of Relativity is Albert Einstein and it is 1200 pages.

Hence you can see how an object and its properties can be useful at times.

The greatest drawback of this technique is that, for every single object that we create under a class, we have to define its properties again and again, individually and this makes our code very long. One way to shorten this is the use of CONSTRUCTORS which will be discussed in the next post.

That is all for this blog!

THANK YOU.

 
Share this