Object Oriented Programming in PHP

Object Oriented Programming (OOP) is a programming technique to divide similar tasks into Class and Object to create reusable code. As we can imagine things as different objects like car made of different objects like wheel, body, steering, gear etc. The car object has properties and methods. The properties includes features like body color, number of doors, shape, brand and model. The methods have functionalities the object can do. For example a car can start, stop, open doors, drive etc. So as we progress, we will see how properties and methods can be represented as reusable and cut down the code and time.

Also, read:

Object Oriented Programming Concepts in PHP:

Now we will explain Object Oriented Programming concepts in details with example.

Define Class

A class hold the definition of objects data and methods or we can that the class contains the method and properties of the object. So here we will see how it handles everything altogether. Here we have defined a class Car. All the car have similar characteristics to get car details like car model, color, brand etc. So these can be defined as method and data members.

<?php
class Car {
/* Member variables */
var $price;
var $color;
/* Member functions */
function setPrice($car_price){
$this->price = car_price;
}
/* Member functions */
function getPrice(){
echo $this->price;
}
/* Member functions */
function setColor($car_color){
$this->color = $car_color;
}
/* Member functions */
function getColor(){
echo $this->color;
}
}
?>

The word class is used to define class followed by the class name. We use first letter of the class name as capital letter. The variable $this is a variable and it refers to the same object or itself.

Create Object

After defining class, we can create multiple objects of same class Car according to our requirement. The new keyword is used to create a new object of a class. So let’s see how it works.

<?php
/* create a new car object */
$carObj = new Car();
?>

Call Member Methods

After creating Car object, we will call methods of created object. So first we will call set methods to set Car price and color by calling member methods.

<?php
/* set the price of the car */
$carObj->setPrice(500000);
/* set the color of the car */
$carObj->setColor("red");
?>

Now we will call member methods to get the car price and color by calling member methods.

<?php
/* get the price of the car */
echo "The Car price: ".$carObj->getPrice();
/* get the color of the car */
echo "The Car color: ".$carObj->getColor();
?>

The above member methods will return car price and color as below:

The Car price: 500000
The Car color: Red



Inheritance

Inheritance is the main feature of object oriented programming. Inheritance is used to inherit definition of parent class using extends clause to child class means child class has all the characteristics of the parent class.

Here we have a class Vehicle with method getBrand().

<?php
class Vehicle {
public $brand;
public function __construct(){
echo 'Vehicle Details';
}
public function getBrand() {
return $this->$brand;
}
}
?>

Now we will inherit class Vehicle to class child class Car and then create object of Car and call set data to parent call variable and call method.

<?php
class Car extends Vehicle {
public function __construct(){
echo 'Car Details';
}
}
$carObj = new Car();
$carObj->brand = 'BMW';
echo "The Car brand is: ".$carObj->getBrand();
?>

The above will result following:

The Car brand is: BMW

Public, Private and Protected Members

By default the visibility of class members are public. It means the properties and methods of a class are public and can be accessed within and from outside the class in which it is declared. The public members are also accessible within another class in which it is implemented or inherited. The above inheritance examples are public. We can make members public by using public keyword with member variables and methods.

The private class members have limited access. The private class members are accessible within the class in which it is declared. It can not be accessed outside of class or from classes that inherit it. We can make class members and method as private by using private keyword. Let’s see this in example below.

<?php
class Car {
private $price;
public function setPrice($car_price) {
$this->$price = $car_price;
}
}
$carObj = new Car();
$carObj->setPrice(600000);
?>

In above example if we try to set the member variable $price without setPrice() method, it will return Fatal Error related to not access private property since the member variable $price is private. Also if we try to access private member variable in child class by inheriting parent class, it’s still fail because the private member variables and methods in parent class are not accessible to child class.

The protected class members are accessible within the class in which it is declared and also in class that extend that class. We can make a class members protected by using keyword protected. Let’s see this in example below.

<?php
class Vehicle {
protected $brand;
public function setBrand($car_brand) {
return $this->$brand = $car_brand;
}
}
class Car extends Vehicle {
public function getBrand() {
echo "The car brand is: ".$brand;
}
}
$carObj = new Car();
$carObj->setBrand("BMW");
$carObj->getBrand();
?>

In above example the parent class method setBrand() called by child class object to set brand. Here we are able to do this because the the member variable $brand is declared as protected and accessible to the child class.



Final Keyword

As we have learn to protect code by using private and protected keywords, you can also define class or method with final keyword to protect code. If a class or method is declared with final can not be override or inherited by another class. Let’s see this in example.

<?php
final class Vehicle {
}
class Car extends Vehicle {
}
?>

In above code, we have a class Vehicle with final keyword. If we try to inherit this class to another class then it will return Fatal Error related to final class may not inherited. This is useful when we wants to code to protect from other use.

You may also like:

2 thoughts on “Object Oriented Programming in PHP

Comments are closed.