Learn PHP Tutorial

PHP OOP – Classes and Objects


A class is a template which determine how an objects will behave and what the objects will contain. A class is a blueprints for making instances of the same kind (or class) of object.

Let us understand with the help of some examples:

Example 1 – Suppose there is a builder and he has a map and using that map, he wants to builds flats. Here, map is a blueprints (class) and with the help of that map (class) he will create many flats (objects).

Example 2 – Suppose there is a car manufacturing company. A car manufacturing company will have a common or basic template to manufacture different different cars.

Here basic template means, all features and details that should be in a car.
– Color
– Engine
– Seats
– AC
– Price

These are the basic things available in each cars. So, with the help of this template, manufacturing company can decide and make several types of cars with different features and different values for the properties.

Maruti SwiftToyota Fortuner
ColorWhiteBlack
Engine1200 CC2700 CC
Seats57
ACYesYes
priceRs.5,50,000Rs.28,00,000

How to define a class ?

A class is defined by the “class” keyword, followed by the name of that class and a pair of curly braces ({}). A class includes methods and properties. In class definition, a function is called method and variables are called properties.

Syntax

<?php
class Cars 
{
   //code goes here...
}
?>

Now we can declare two properties in class “Car” ($name and $color) and two methods set_name() and get_name() for setting and getting the car $name property.

<?php
class Cars 
{
   //Properties..
   public $name;
   public $color;

   //Methods...
   function set_name($name)
   {
       $this->name = $name;
   }

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

How to define an object?

Objects are the instance of a class. An object can not be created without class and a class is nothing until an object is created. We can create multiple objects from a class. Each object has all the properties and methods but values of properties might differ in each object according to object feature.

Let us understand with an example of two different cars. Maruti Swift and Toyota Fortuner both are the instances of class Car:

<?php
class Cars 
{
   //Properties..
   public $name;
   public $color;

   //Methods...
   function set_name($name)
   {
       $this->name = $name;
   }

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

//Objects...
$car1 = new Car();
$car2 = new Car();

//Setting name for each object (car1 and car2)...
$car1->set_name('Maruti Swift');
$car2->set_name('Toyota Fortuner');

echo $car1->get_name;
echo "<br>";
echo $car2->get_name();
?>

//OUTPUT
Maruti Swift
Toyota Fortuner

Let us add one more methods for setting and getting the color of each car.

<?php
class Cars 
{
   //Properties..
   public $name;
   public $color;

   //Methods...
   function set_name($name)
   {
       $this->name = $name;
   }

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

   function set_color($color)
   {
       $this->color= $color;
   }

   function get_color()
   {
      return "This car color is ". $this->color;
   }

}

//Objects...
$car1 = new Car();
$car2 = new Car();

//Setting name for each object (car1 and car2)...
$car1->set_name('Maruti Swift');
$car2->set_name('Toyota Fortuner');

//Setting property color for each car...
$car1->set_color('white');
$car2->set_color('black');

echo $car1->get_name;
echo "<br>";
echo $car1->get_color;

echo "<br><br>";

echo $car2->get_name();
echo "<br>";
echo $car2->get_color;
?>

//OUTPUT
Maruti Swift
This car color is white

Toyota Fortuner
This car color is black

PHP – The $this keyword

The variable $this is a special variable and it refers to the current object ie. itself and it is only available inside the methods.

Let us understand with the help of some example.

In order to change the car name property, there are two ways.

Inside the class by adding a set_name() method using $this variable.

<?php
class Cars 
{
   //Properties..
   public $name;

   //Methods...
   function set_name($name)
   {
       $this->name = $name;
   }
}

$car1 = new Car();
$car1->set_name('Maruti Suzuki Dzire')
?>

Outside the class – we can change the the property value even directly.

<?php
class Cars 
{
   //Properties..
   public $name;
}

$car1 = new Car();
$car1->name('Maruti Suzuki Dzire')
?>

PHP – instanceof keyword

If we want to check which specific class an object belongs to, then we can use instanceof php keyword. It will return a Boolean value “TRUE” if an object belongs to specified class otherwise “FALSE”.

<?php
$car1 = new Car();
var_dump($car1 instanceof Car);
?>

//OUTPUT
bool(true)