Abstract vs Interface in PHP

Interface

  • Allow define methods should be implement in a class
  • When one or more classes use same interface, it’s called “polymorphism”

What interface does

  • Can’t have properties
  • All methods must be public
  • By default all methods are abstract

Abstract

  • A class with abstract methods, and need its children classes to fill out
  • Abstract method is a method that is declared but not implemented

So what’s different between Abstract vs Interface

  • Interface can’t have properties or any kind of data member but Abstract can
  • Interface have only public methods but Abstract can have both public / protected and even private
  • Methods interaface are abstract, no abstract keyword required
  • Class can extend from 1 Abstract but can implement from multi interfaces

When will we use Interfaces

  • When we have different classes but same interface(s)
    • Database
    • Caching
    • Payments
Read more: Abstract vs Interface in PHP

Sample code ( By ChatGPT )

interface PaymentGateway {
  public function authorizePayment();
  public function capturePayment();
}

class Payment {
  protected $amount;
  protected $currency;
  
  public function __construct($amount, $currency) {
    $this->amount = $amount;
    $this->currency = $currency;
  }
  
  // other methods
}

abstract class Service {
  protected $name;
  protected $price;
  
  public function __construct($name, $price) {
    $this->name = $name;
    $this->price = $price;
  }
  
  public function getName() {
    return $this->name;
  }
  
  public function getPrice() {
    return $this->price;
  }
  
  abstract public function processService();
}

class ProductService extends Service {
  public function processService() {
    // implementation for processing a product service
  }
}

class SubscriptionService extends Service {
  public function processService() {
    // implementation for processing a subscription service
  }
}

class CreditCardPayment implements PaymentGateway {
  protected $cardNumber;
  protected $expirationDate;
  protected $cvv;
  
  public function __construct($cardNumber, $expirationDate, $cvv) {
    $this->cardNumber = $cardNumber;
    $this->expirationDate = $expirationDate;
    $this->cvv = $cvv;
  }
  
  public function authorizePayment() {
    // implementation for authorizing a credit card payment
  }
  
  public function capturePayment() {
    // implementation for capturing a credit card payment
  }
  
  // other methods
}

class PayPalPayment implements PaymentGateway {
  protected $username;
  protected $password;
  
  public function __construct($username, $password) {
    $this->username = $username;
    $this->password = $password;
  }
  
  public function authorizePayment() {
    // implementation for authorizing a PayPal payment
  }
  
  public function capturePayment() {
    // implementation for capturing a PayPal payment
  }
  
  // other methods
}

class PaymentService {
  protected $gateway;
  
  public function __construct(PaymentGateway $gateway) {
    $this->gateway = $gateway;
  }
  
  public function processPayment(Payment $payment, Service $service) {
    $this->gateway->authorizePayment();
    $this->gateway->capturePayment();
    $service->processService();
  }
  
  public function setGateway(PaymentGateway $gateway) {
    $this->gateway = $gateway;
  }
}

// create a credit card payment gateway
$creditCard = new CreditCardPayment('1234567890123456', '12/22', '123');

// create a payment service with the credit card payment gateway
$paymentService = new PaymentService($creditCard);

// process a payment for a product service
$productService = new ProductService('Product Service', 19.99);
$payment = new Payment(19.99, 'USD');
$paymentService->processPayment($payment, $productService);

// switch to PayPal payment gateway
$paypal = new PayPalPayment('username', 'password');
$paymentService->setGateway($paypal);

// process a payment for a subscription service
$subscriptionService = new SubscriptionService('Subscription Service', 9.99);
$payment = new Payment(9.99, 'USD');
$paymentService->processPayment($payment, $subscriptionService);

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: