Abstraction in PHP
Abstraction in programming is a principle that allows you to reduce complexity by hiding unnecessary details and showing only the essential features of an object or concept. In object-oriented programming, abstraction is achieved using classes and objects.
Here’s a simple example of abstraction using PHP:
Example: Bank Account
Imagine you are creating a banking system. You want to create a class that represents a bank account. The user of this class does not need to know how the balance is stored or how transactions are processed. They just need to know how to deposit, withdraw, and check the balance.
<?php
// Abstract class
abstract class BankAccount {
// Private properties to store the balance
private $balance;
// Constructor to initialize the balance
public function __construct($initialBalance) {
$this->balance = $initialBalance;
}
// Abstract methods to be implemented by subclasses
abstract protected function deposit($amount);
abstract protected function withdraw($amount);
// Concrete method to get the current balance
public function getBalance() {
return $this->balance;
}
// Protected method to set the balance
protected function setBalance($amount) {
$this->balance = $amount;
}
}
// Subclass of BankAccount
class SavingsAccount extends BankAccount {
// Implement the deposit method
public function deposit($amount) {
$newBalance = $this->getBalance() + $amount;
$this->setBalance($newBalance);
echo "Deposited: $amount. New Balance: " . $this->getBalance() . "\n";
}
// Implement the withdraw method
public function withdraw($amount) {
if ($amount > $this->getBalance()) {
echo "Insufficient funds. Current Balance: " . $this->getBalance() . "\n";
} else {
$newBalance = $this->getBalance() - $amount;
$this->setBalance($newBalance);
echo "Withdrew: $amount. New Balance: " . $this->getBalance() . "\n";
}
}
}
// Using the SavingsAccount class
$account = new SavingsAccount(100);
$account->deposit(50); // Output: Deposited: 50. New Balance: 150
$account->withdraw(30); // Output: Withdrew: 30. New Balance: 120
$account->withdraw(200); // Output: Insufficient funds. Current Balance: 120
?>
Explanation
- Abstract Class:
BankAccount
is an abstract class that defines the basic structure of a bank account. - Abstract Methods:
deposit
andwithdraw
are abstract methods that must be implemented by any subclass ofBankAccount
. - Concrete Methods:
getBalance
andsetBalance
are concrete methods that provide functionality to get and set the balance. - Subclass:
SavingsAccount
is a subclass ofBankAccount
that provides implementations for thedeposit
andwithdraw
methods.
By using abstraction, we define a clear interface for bank accounts and hide the implementation details, making the code easier to understand and maintain.
In this point, you can think abstraction is very similar to inheritance. now let me clear more about that.
Abstraction and inheritance are closely related concepts in object-oriented programming. Here’s how they relate and differ:
Inheritance
Inheritance allows a class (subclass or child class) to inherit properties and methods from another class (superclass or parent class). It promotes code reusability and establishes a relationship between classes.
Abstraction
Abstraction, on the other hand, focuses on hiding the complex implementation details and exposing only the necessary parts. It helps in reducing complexity and allows the programmer to focus on interactions at a higher level.
Combined Usage
Often, abstraction and inheritance are used together to design systems that are both modular and easy to extend. The example provided earlier showcases both:
- Abstraction: The
BankAccount
class defines the abstract methodsdeposit
andwithdraw
, which must be implemented by subclasses. This hides the complexity of these operations from the user of theBankAccount
class. - Inheritance: The
SavingsAccount
class inherits fromBankAccount
and provides specific implementations for the abstract methods.
Here’s a simpler example to illustrate the concepts clearly:
Example: Vehicle
Abstract Base Class
<?php
abstract class Vehicle {
// Abstract method
abstract protected function startEngine();
// Concrete method
public function describe() {
echo "This is a vehicle.\n";
}
}
class Car extends Vehicle {
// Implement the abstract method
public function startEngine() {
echo "Car engine started.\n";
}
}
class Motorcycle extends Vehicle {
// Implement the abstract method
public function startEngine() {
echo "Motorcycle engine started.\n";
}
}
// Using the classes
$car = new Car();
$car->describe(); // Output: This is a vehicle.
$car->startEngine(); // Output: Car engine started.
$motorcycle = new Motorcycle();
$motorcycle->describe(); // Output: This is a vehicle.
$motorcycle->startEngine(); // Output: Motorcycle engine started.
?>
Explanation
- Abstract Class:
Vehicle
is an abstract class that declares an abstract methodstartEngine
and a concrete methoddescribe
. - Inheritance:
Car
andMotorcycle
are subclasses ofVehicle
and inherit thedescribe
method. - Abstraction:
Car
andMotorcycle
provide their own implementations of thestartEngine
method, hiding the details of how each type of vehicle starts its engine.
By using abstraction and inheritance, we can create a flexible and reusable code structure where different types of vehicles share common behavior (inherited from Vehicle
) while also providing their specific implementations of abstract methods.
Hope you found this helpful!