Classes in PHP

Object Oriented Concepts

In Object Oriented PHP concepts, we assume everything as an object and implement a software using objects (using classes). A class is a template for making many instances of the same kind of object. An obect is an indivudual instance of the data structure defined by the class. We define a class once and then create objects belonging to it.

Defining a Class in PHP

The general format of defining a class in PHP is:

<?php
    class book {

        //member variables
        public $name;
        public $price;

        //member functions
        public function display() {
            echo "<br/>Name: ".$this->name;
            echo "<br/>Price: ".$this->price;
        }

    }
?>

Description:

  • A class is defined by the name of the class you want.
  • Member variables are the variables defined in the class. There are three access modifiers namely: public, private and protected.
    • Public: Public members are accessible from outside the class. By default all the members are public unless specified.
    • Private: Private members are not accessible from outside the class. They are also not inherited to the child class. (We will talk about inheritence later).
    • Protected: Protected members are also not accessible outside the class, however they are inherited to the child class.
  • Member functions are functions defined inside a class and are used to access object data. The variable $this in the display() function is a special variable and it refers to the same object that is itself.

Creating Objects in PHP

<?php
    $physics = new book;

    $physics->name = "Physics";
    $physics->price = 200;

    $physics->display();
?>

Capture1

Description

  • $physics is an object of the class book.
  • Its name and price have been initialized as “Physics” and “200”.
  • The display function is called for $physics.

Constructors

A Constructor is called automatically when the object is created. PHP provides a special function to define a constructor. For example:

<?php

    function __construct($name, $price) {
        $this->name = $name;
        $this->price = $price;
    }
?>

By using a constructor, we don’t need to set the indivudual member variables separately. We can initialize them while creating the object itself.

<?php

    $physics = new book("Physics", 200);
    $chemistry = new book("Chemistry", 250);

?>

When the $physics object is created, its name and price are automatically initialized as “Physics” and “200” respectively.

<?php
    class book {

        public $name;
        public $price;
        public $quantity;

        function __construct($name, $price) {
            $this->name = $name;
            $this->price = $price;
            $this->quantity = 0;
        }

        public function display() {
            echo "<br/>Name: ".$this->get_name();
            echo "<br/>Price: ".$this->get_price();
            echo "<br/>Quantity: ".$this->get_quantity();
        }

        public function set_quantity($quantity) {
            $this->quantity = $quantity;
        }

        public function get_quantity() {
            return $this->quantity;
        }

        public function get_name() {
            return $this->name;
        }

        public function get_price() {
            return $this->price;
        }
    }
?>

In the above code, you can observe that the constructor initializes the quantity for any new object to 0. There is no ‘quantity’ argument passed in the constructor. Another function set_quantity() us used to set the quantity member variable of the object.

<?php
    include('class.books.php');
    $physics = new book("Physics", 200);
    $physics->display();
    echo '<br>';
    $physics->set_quantity(5);
    $physics->display();
?>

Now this will give the following output:
Capture

Inheritance

A class can inherit the properties of another class. For example, let us suppose we have a class mammal. We have two other classes named human and monkey. Now, both human and monkey are mammals, thus they will be sharing some common attributes together. It will be a tedious task to create the some attributes (ie. member variables and member functions) for both human and monkey. Thus, it will be an easier task to create a class called mammal, and let human and monkey inherit properties from mammal.

The class which inherits the properties is called a child class, and the class which is being inherited is called the parent class.
The syntax for inheritance is as follows:

<?php
    class child extends parent {
        //class body
    }
?>

For example:

<?php
    class novel extends book {
        public $edition;

        public function set_edition($edition) {
            $this->edition = $edition;
        }
    }
?>

In the above example, the class novel inherits the properties of the class book. As a result, an object of novel will have all the public and protected member variables and functions of the class book and additionally will also have a member variable called edition.

<?php
    $manga = new novel("Manga", 20);
    $manga->display();
?>

This will produce the following result:

Name: Manga
Price: 20
Quantity: 0

Function Overriding

In the above example, when we call the display function for manga object, automatically the parent class(book)’s display function is called. Now, how do we display the edition for the novel. The solution is Function Overriding.
Function definitions in child classes override definitions with the same name in parent classes. In a child class, we can modify the definition of a function inherited from parent class.

<?php
    class novel extends book {
        public $edition;

        public function set_edition($edition) {
            $this->edition = $edition;
        }

        public function display() {
            parent::display();
            echo "<br/>Edition: ".$this->edition;
        }
    }
?>

In the above code, the display function of novel first calls the parent’s display function and then prints the novel’s variable edition.

<?php
    $manga = new novel("Manga", 20);
    $manga->set_edition(3);
    $manga->display();
?>

This produces the following output:

Name: Manga
Price: 20
Quantity: 0
Edition: 3

Please feel free to download the source code for reference.

View Demo Download source

This content has been helpful to you?

Thanks for contributing!

Yes No