An interface is a form of constraint that includes only member definitions and does not contain content that is implemented by members. with interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specific contents of those methods. We can define an interface by interface, just like defining a standard class, but all of the methods defined are empty.
All methods defined in an interface must be public, which is an attribute of the interface.
Implement
To implement an interface, you can use the implements operator. Class must implement all the methods defined in the interface, otherwise a fatal error will be reported. If you want to implement multiple interfaces, you can use commas to separate the names of multiple interfaces.
Note:
When implementing multiple interfaces, the methods in the interface cannot have duplicate names.
Note:
Interfaces can also be inherited by using the extends operator.
Constants
You can also define constants in an interface. Interface constants and class constants are used exactly the same. They are all fixed values and cannot be modified by quilts or sub-interfaces.
Example
Example #1 Interface code example
<?php
Declare a ' ITemplate ' interface
Interface ITemplate
{
Public Function SetVariable ($name, $var);
Public Function gethtml ($template);
}
Implementing interfaces
The following is the correct wording
Class Template implements ITemplate
{
Private $vars = Array ();
Public Function SetVariable ($name, $var)
{
$this->vars[$name] = $var;
}
Public Function gethtml ($template)
{
foreach ($this->vars as $name => $value) {
$template = Str_replace (' {'. $name. '} ', $value, $template);
}
return $template;
}
}
The following wording is wrong and will be an error:
Fatal Error:class Badtemplate contains 1 abstract methods
and must therefore be declared abstract (itemplate::gethtml)
Class Badtemplate implements ITemplate
{
Private $vars = Array ();
Public Function SetVariable ($name, $var)
{
$this->vars[$name] = $var;
}
}
?>
Example #2 Extendable Interfaces
<?php
Interface A
{
Public function foo ();
}
Interface B extends A
{
Public function Baz (Baz $baz);
}
Correct wording
Class C Implements B
{
Public Function foo ()
{
}
Public function Baz (Baz $baz)
{
}
}
Wrong writing can result in a fatal error
Class D implements B
{
Public Function foo ()
{
}
Public function Baz (Foo $foo)
{
}
}
?>
Example #3 inheritance between multiple interfaces
<?php
Interface A
{
Public function foo ();
}
Interface B
{
Public function bar ();
}
Interface C extends A, b
{
Public function Baz ();
}
Class D implements C
{
Public Function foo ()
{
}
Public Function Bar ()
{
}
Public Function Baz ()
{
}
}
?>
Example #4 Using Interface constants
<?php
Interface A
{
Const B = ' Interface constant ';
}
Output Interface Constants
Echo a::b;
Incorrectly, because the value of a constant cannot be modified. The concept of an interface constant is the same as a class constant.
Class B implements a
{
Const B = ' Class constant ';
}
?>