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 cl…


This content originally appeared on DEV Community and was authored by Ghulam Mujtaba

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());

I hope that you have clearly understood it.


This content originally appeared on DEV Community and was authored by Ghulam Mujtaba


Print Share Comment Cite Upload Translate Updates
APA

Ghulam Mujtaba | Sciencx (2024-07-25T12:12:33+00:00) Handshakes and Interfaces in OOP. Retrieved from https://www.scien.cx/2024/07/25/handshakes-and-interfaces-in-oop/

MLA
" » Handshakes and Interfaces in OOP." Ghulam Mujtaba | Sciencx - Thursday July 25, 2024, https://www.scien.cx/2024/07/25/handshakes-and-interfaces-in-oop/
HARVARD
Ghulam Mujtaba | Sciencx Thursday July 25, 2024 » Handshakes and Interfaces in OOP., viewed ,<https://www.scien.cx/2024/07/25/handshakes-and-interfaces-in-oop/>
VANCOUVER
Ghulam Mujtaba | Sciencx - » Handshakes and Interfaces in OOP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/25/handshakes-and-interfaces-in-oop/
CHICAGO
" » Handshakes and Interfaces in OOP." Ghulam Mujtaba | Sciencx - Accessed . https://www.scien.cx/2024/07/25/handshakes-and-interfaces-in-oop/
IEEE
" » Handshakes and Interfaces in OOP." Ghulam Mujtaba | Sciencx [Online]. Available: https://www.scien.cx/2024/07/25/handshakes-and-interfaces-in-oop/. [Accessed: ]
rf:citation
» Handshakes and Interfaces in OOP | Ghulam Mujtaba | Sciencx | https://www.scien.cx/2024/07/25/handshakes-and-interfaces-in-oop/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.