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.

<?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 = '[email protected]';
        $newsletter ->subscribe($email);
    }}
$controller = new NewsletterSubscriptionsController(); $controller->store(new CompaignMonitor()); 
Enter fullscreen mode Exit fullscreen mode

I hope that you have clearly understood it.


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Bare metal dedicated servers. Trusted restaurant review & rating.