Interfejsy
Interfejs to szablon składający się ze stałych i deklaracji metod, które muszą być użyte w klasach, które go zaimplementują. Interfejs deklarujemy modyfikatorem interface:
1 2 3 |
interface InterfaceName { // deklaracja stałych i/lub metod } |
Dana klasa może implementować wiele interfejsów:
1 2 3 |
class ClassName implements Interface1, Interface2, Interface3... { // ... } |
Interfejsy mogą dziedziczyć inne interfejsy:
1 2 3 |
interface InterfaceName extends Interface1, Interface2, Interface3... { // ... } |
Przykład interfejsu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php // Connection.php interface Connection { const HOST = 'localhost'; const PORT = 22; const USERNAME = 'root'; const PASSWORD = '**********'; const DBNAME = 'database'; function linkDB(); } ?> |
Jak widać, jest to interfejs służący do połączenia się z serwerem bazy danych. Zgodnie z …