Interfejsy PHP
W PHP interfejsy pozwalają określić, jakie metody powinna mieć klasa, ale bez implementacji tych metod. Przykład (interfejs aplikacji Zend3):
<?php
namespace Zend\Mvc;
interface ApplicationInterface
{
// Retrieves the service manager.
public function getServiceManager();
// Retrieves the HTTP request object.
public function getRequest();
// Retrieves the HTTP response object.
public function getResponse();
// Runs the application.
public function run();
}
Jak widać na powyższym przykładzie, interfejs jest definiowany za pomocą słowa kluczowego interface
, prawie w ten sam sposób, w jaki definiujesz standardową klasę PHP.
Klasa implementująca interfejs nazywana jest konkretyzacją.
Przykład:
<?php
namespace Zend\Mvc;
class Application implements ApplicationInterface
{
// Implement the interface's methods here
public function getServiceManager()
{
// Provide some implementation...
}
public function getRequest()
{
// Provide some implementation...
}
public function getResponse()
{
// Provide some implementation...
}
public function run()
{
// Provide some implementation...
}
}
Konkretyzację interfejsu definiuje słowo kluczowe implements
.