Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as "polymorphism".
Interfaces are declared with the interface keyword:
Syntax
interface InterfaceName {
public function someMethod1();
public function someMethod2($name, $color);
public function someMethod3() : string;
}
?>
PHP - Interfaces vs. Abstract Classes
Interface are similar to abstract classes. The difference between interfaces and abstract classes are:
- Interfaces cannot have properties, while abstract classes can
- All interface methods must be public, while abstract class methods is public or protected
- All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary
- Classes can implement an interface while inheriting from another class at the same time
PHP - Using Interfaces
To implement an interface, a class must use the implements keyword.
A class that implements an interface must implement all of the interface's methods.
Example
interface Animal {
public function makeSound();
}
class Cat implements Animal {
public function makeSound() {
echo "Meow";
}
}
$animal = new Cat();
$animal->makeSound();
?>
From the example above, let's say that we would like to write software which manages a group of animals. There are actions that all of the animals can do, but each animal does it in its own way.
Using interfaces, we can write some code which can work for all of the animals even if each animal behaves differently:
Example
// Interface definition
interface Animal {
public function makeSound();
}
// Class definitions
class Cat implements Animal {
public function makeSound() {
echo " Meow ";
}
}
class Dog implements Animal {
public function makeSound() {
echo " Bark ";
}
}
class Mouse implements Animal {
public function makeSound() {
echo " Squeak ";
}
}
// Create a list of animals
$cat = new Cat();
$dog = new Dog();
$mouse = new Mouse();
$animals = array($cat, $dog, $mouse);
// Tell the animals to make a sound
foreach($animals as $animal) {
$animal->makeSound();
}
?>
Example Explained
Cat, Dog and Mouse are all classes that implement the Animal interface, which means that all of them are able to make a sound using the makeSound() method. Because of this, we can loop through all of the animals and tell them to make a sound even if we don't know what type of animal each one is.
Since the interface does not tell the classes how to implement the method, each animal can make a sound in its own way.
Tutorial OOP PHP – Abstract Class dan Interface, jika Kalian tau Polymorphism Abstract dan Interface digunakan untuk itu. Jadi Apa Itu Polymorphism ?, Polymorphism adalah konsep dalam OOP dimana class-class secara fungsional memiliki fungsi yang berbeda tapi mempunyai Interface yang sama.
Kita ambil contoh Smartphone, sistem operasi smartphone ada Android, iOS, dan Windows Phone secara interface Smartphone punya method OS.
Jika dikonversi dalam coding
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
interface Smartphone {
public function os();
}
class Nokia implement SmartphoneThisClassIsTooLongForTheNamed {
public function os() {
echo 'Windows Phone';
}
}
class Samsung implement Smartphone
{
public function os()
{
echo 'Android';
}
}
Bisa kita lihat diClass Nokia dan Samsung, sama-sama menggunakan interface Smartphone dan sama-sama menggunakan method os() tapi memiliki output yang berbeda.
Baca Juga : PHP Object Oriented Programming : Class Extends
Cara Membuat Interface
Seperti yang telah dicontohkan di atas, interface sangat mirip dengan class hanya saja dimulai dengan Keyword interface lalu diikuti nama interface misalnya Smartphone.
Interface tidak dapat di instance sebagaimana class pada umum, selain itu interface hanya memiliki method tanpa body, seperti method os() pada interface Smartphone.
Cara Menggunakan Interface pada Class
Tidak seperti class yang di extends, untuk menggunakan interface kita bisa menggunakan keyword implements pada class yang ingin menggunakan interface.
contoh
1
2
3
4
class Samsung Implements Smartphone
{
}
Menggunakan Lebih dari Satu Interface
1
2
3
4
class Samsung Implements Smartphone, Tablets, MiniPC
{
}
Dan setiap Class wajib mendefinisikan Ulang semua method yang ada di dalam interface.
Selain dengan Interface kita juga bisa menggunakan Abstract Class.
Cara Membuat Abstract Class
Abstract Class hampir sama dengan class pada umumnya hanya saja ada penambahan Keyword Abstract dan tidak bisa di-instance, sehingga class abstract ini harus di extends pada class yang ingin digunakan.
Contoh
1
2
3
abstract class Smartphone {
}
Karena hampir mirip class pada umumnya Abstract Class juga memiliki properties dan method dengan body. Dan ada penambahan abstract method, dimana abstract method ini tidak memiliki body dan wajib ditulis ulang pada kelas di extends.
contoh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
abstract class Smartphone
{
protected $version='1.0';
public function __construct()
{
echo 'construct abstract';
}
abstract public function os();
public function getVersion()
{
echo $this->version;
}
}
class Samsung extends Smartphone
{
public function os()
{
echo 'android';
}
}
$hh = new Samsung();
$hh->os();
$hh->getVersion();
Nah dengan menggunakan abstract dan atau interface kita bisa menggunakan fitur polymorphism dari OOP, hal yang paling mencolok Dari abstract dan interface adalah, sebuah class tidak bisa meng-extends lebih dari satu abstract. Tapi satu class bisa meng-implements lebih dari satu interface.
Baik, saya rasa cukup untuk abstract dan interface-nya. artikel berikutnya adalah tentang contoh php class. Disana nanti kita akan belajar membuat Class untuk menangani CRUD. Jangan sampai lupa yaa.