PHP 8 - Object-oriented programming (OOP)

Last modified: April 02, 2022

The three major principles of OOP are;

  • Encapsulation This concept is also often used to hide the internal representation, or state of an object from the outside
  • Inheritance Is a concept that acquires the properties from one class to other classes
  • Polymorphism Is the ability of an object to take many forms
How implement Inheritance in PHP

In the below diagram, we can see Car and Truck inherit from the Vechicle class

sql

Let's see how we code in PHP. Create below PHP files

Vehicle.php
<?php class Vehicle { private $numberOfWheels; private $cost; public function __construct($numberOfWheels, $cost) { $this->numberOfWheels = $numberOfWheels; $this->cost = $cost; } public function get_numberOfWheels() { return $this->numberOfWheels; } public function set_numberOfWheels($numberOfWheels) { $this->numberOfWheels= $numberOfWheels; } public function get_cost() { return $this->cost; } public function set_cost($cost) { $this->cost= $cost; } } ?>
Car.php
<?php class Car extends Vehicle { private $isSportCar; public function __construct($numberOfWheels, $cost) { parent::__construct($numberOfWheels, $cost); } public function set_isSportCar($isSportCar) { $this->isSportCar = $isSportCar; } public function get_isSportCar() { return $this->isSportCar; } } ?>
Truck.php
<?php class Truck extends Vehicle { public function __construct($numberOfWheels, $cost) { parent::__construct($numberOfWheels, $cost); } } ?>
Index.php
<?php require 'Vehicle.php'; require 'Car.php'; require 'Truck.php'; $car = new Car(4, 2900); $truck = new Truck(4, 2000); echo 'Car has ' . $car->get_numberOfWheels(). ' wheels and price is '. $car->get_cost() . '<br /><br />'; echo 'Truck has ' . $truck->get_numberOfWheels(). ' wheels and price is '. $truck->get_cost(); ?>
Test the application

sql

How implement Polymorphism in PHP

New techonology every comes frequently, suppose you have PHP application that connects MySQL as database first. Few year you decided to change the Database to Microsoft SQL Server.

We can create an interface that defines signature of class and an abstract class that implements it.

<?php interface DatabaseInterface { public function db_connect(); public function add($data); public function get($where); public function update($where); public function remove($where); } ?>
<?php abstract class DatabaseMethods { private $host; private $db; private $uid; private $password; public function __construct($host, $db, $uid, $password) { $this->host = $host; $this->db = $db; $this->uid = $uid; $this->password = $password; } } ?>
MySQLDriver.php
<?php class MySQLDriver extends DatabaseMethods implements DatabaseInterface { public function __construct($host, $db, $uid, $password) { parent::__construct($host, $db, $uid, $password); } public function db_connect() { } public function remove($where) { } public function add($data) { } public function get($where) { } public function update($where) { } } ?>
SQLServerDriver.php
<?php class SQLServerDriver extends DatabaseMethods implements DatabaseInterface { public function __construct($host, $db, $uid, $password) { parent::__construct($host, $db, $uid, $password); } public function db_connect() { } public function remove($where) { } public function add($data) { } public function get($where) { } public function update($where) { } } ?>
Example
<?php $db = new SQLServerDriver($host,$db,$uid,$password); $db->db_connect(); $db->add($data); ?>