Interfejsy w PHP

Autor podstrony: Krzysztof Zajączkowski

Stronę tą wyświetlono już: 3828 razy

W PHP istnieje możliwość dziedziczenie jedynie po jednej klasie (nie ważne czy abstrakcyjnej czy nie). Z tego też względu wprowadzono możliwość tworzenia interfejsów, które mają na celu wymuszenie na każdej klasie z nimi związanej obsługę danego zestawu metod. Jedna klasa może dziedziczyć wiele interfejsów. Interfejsy tworzy się wykorzystując słowo kluczowe interface, natomiast dołącza się je do danej klasy za pomocą słowa kluczowego implements. Oto przykład interfejsu i klas z niego korzystających:

<?php interface iArea{ // interfejs wymuszający na obiektach go implementujących obsługę metod w nim zadeklarowanych public function getTop(); public function getBottom(); public function getLeft(); public function getRight(); } // Tworzenie klasy abstrakcyjnej figura abstract class Figure implements iArea{ // a tutaj implementacja interfejsu w klasie abstrakcyjnej Figure private static $nrOfFigures; // ta zmienna statyczna klasy będzie przechowywała liczbę aktualnie utworzonych figur geometrycznych protected $fillColor; protected $strokeColor; public static function getNrOfFigures(){ return self::$nrOfFigures; } private $figureName; public function __construct($fillColor = "#ffffff", $strokeColor = "#000000"){ self::$nrOfFigures ++; // tutaj zwiększam licznik utworzonych figur o 1 $this->fillColor = $fillColor; $this->strokeColor = $strokeColor; echo("<p>(Dodawanie) Aktualna liczba utworzonych figur geometrycznych: " . self::$nrOfFigures . "</p>"); } public function __destruct(){ self::$nrOfFigures --; // tutaj zminiejszam licznik utworzonych figur o 1 echo("<p>(Usuwanie) Aktualna liczba utworzonych figur geometrycznych: " . self::$nrOfFigures . "</p>"); } public function setFillColor($fillColor){ $this->fillColor = $fillColor; } public function setStrokeColor($strokeColor){ $this->strokeColor = $strokeColor; } public function getFillColor(){ return $this->fillColor; } public function getStrokeColor(){ return $this->strokeColor; } abstract public function draw(); // metoda abstrakcyjna rysuj abstract public function area(); // metoda abstrakcyjna pole powierzchni abstract public function circuit(); // metoda abstrakcyjna obwód } class Circle extends Figure{ protected $xc, $yc, $ray; public function __construct($xc, $yc, $ray, $name = "", $fillColor = "#ffffff", $strokeColor = "#000000"){ parent::__construct($fillColor, $strokeColor); $this->name = $name; $this->xc = $xc; $this->yc = $yc; $this->ray = $ray; } // obsługa metod interfejsu iArea public function getTop(){ return $this->yc - $this->ray; } public function getBottom(){ return $this->yc + $this->ray; } public function getLeft(){ return $this->xc - $this->ray; } public function getRight(){ return $this->yc + $this->ray; } public function draw(){ echo("<p>Koło: xc = " . $this->xc . "; yc = " . $this->yc . "; promień = " . $this->ray . "</p>"); } public function area(){ return pi() * $this->ray * $this->ray; } public function circuit(){ return pi() * 2 * $this->ray; } } class Rectangle extends Figure{ protected $x, $y, $width, $height; public function __construct($x, $y, $width, $height, $name = "", $fillColor = "#ffffff", $strokeColor = "#000000"){ parent::__construct($fillColor, $strokeColor); $this->name = $name; $this->x = $x; $this->y = $y; $this->width = $width; $this->height = $height; } // obsługa metod interfejsu iArea public function getTop(){ if($this->height > 0){ return $this->y; } return $this->y + $this->height; } public function getBottom(){ if($this->height > 0){ return $this->y + $this->height; } return $this->y; } public function getLeft(){ if($this->width > 0){ return $this->x; } return $this->x + $this->width; } public function getRight(){ if($this->width > 0){ return $this->x + $this->width; } return $this->x; } public function draw(){ echo("<p>Koło: x = " . $this->x . "; y = " . $this->y . "; szerokość = " . $this->width . "; wysokość = " . $this->height . "</p>"); } public function area(){ return pi() * $this->ray * $this->ray; } public function circuit(){ return pi() * 2 * $this->ray; } } ?>
Propozycje książek