DEV Community

Ghulam Mujtaba
Ghulam Mujtaba

Posted on

2

Handshakes and Interfaces in OOP

Handshakes

In OOP PHP, "handshakes" refer to the process of ensuring that a class implements a specific interface or extends a specific class. This is done using the implements keyword for interfaces and the extends keyword for inheriting classes.

Interface

An interface in PHP is a contract that specifies a set of methods that must be implemented by any class that implements it. Interfaces are defined using the interface keyword and cannot contain any implementation code.

  • Example
<?php

interface Newsletter{
    public function subscribe($email);
}
class CompaignMonitor implements Newsletter{
    public function subscribe($email){
        die('subscribinng with compaign monitor');
    }
}
class Drip implements Newsletter{
    public function subscribe($email){ 
        die('subscribinng with Drip');
    }}
class NewsletterSubscriptionsController{
    public function store(Newsletter $newsletter){
        $email = 'janesmith@gmail.com';
        $newsletter ->subscribe($email);
    }}
$controller = new NewsletterSubscriptionsController();

$controller->store(new CompaignMonitor());

Enter fullscreen mode Exit fullscreen mode

I hope that you have clearly understood it.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
dawnodegard profile image
dawn marie odegard

I can’t set it up well.

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay