Skip to content

Instantly share code, notes, and snippets.

@kangmasjuqi
Created May 2, 2021 04:34
Show Gist options
  • Save kangmasjuqi/302b7a76fbf59d3cacc3c90a97d70447 to your computer and use it in GitHub Desktop.
Save kangmasjuqi/302b7a76fbf59d3cacc3c90a97d70447 to your computer and use it in GitHub Desktop.
PHP OOP with Interface
<?php
// INTERFACE
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
interface Notif{
public function send($message, $contacts);
}
class EmailNotif implements Notif{
public function send($message, $contacts){
$target = $contacts['email'];
var_dump("EmailNotif : the message `".$message."` delivered to -> ".$target);
}
}
class SMSNotif implements Notif{
public function send($message, $contacts){
$target = $contacts['number'];
var_dump("SMSNotif : the message `".$message."` delivered to -> ".$target);
}
}
class User{
private $data = [];
public function __construct($data){
$this->data = $data;
}
public function send_notif(Notif $notifier){
var_dump("User Class : let's send notif");
$message = "you are registered successfully";
$notifier->send($message, $this->data['contacts']);
}
}
$registration_data = array(
"name" => "marjuqi",
"gender" => "male",
"contacts" => array(
"number" => "0812000000",
"email" => "[email protected]"
)
);
$user = new User($registration_data);
echo '<pre>';
$user->send_notif(new SMSNotif);
$user->send_notif(new EmailNotif);
echo '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment