This section presents a working example of PHP's interface implementation by creating and implementing an interface, named IPillage, that is used to pillage the company:
interface IPillage
{
function emptyBankAccount();
function burnDocuments();
}
This interface is then implemented for use by the Executive class:
class Executive extends Employee implements IPillage
{
private $totalStockOptions;
function emptyBankAccount()
{
echo "Call CFO and ask to transfer funds to Swiss bank account.";
}
function burnDocuments()
{
echo "Torch the office suite.";
}
}
Because pillaging should be carried out at all levels of the company, you can implement the same interface by the Assistant class:
class Assistant extends Employee implements IPillage
{
function takeMemo() {
echo "Taking memo...";
}
function emptyBankAccount()
{
echo "Go on shopping spree with office credit card.";
}
function burnDocuments()
{
echo "Start small fire in the trash can.";
}
}
As you can see, interfaces are particularly useful because, although they define the number and name of the methods required for some behavior to occur, they acknowledge
the fact that different classes might require different ways of carrying out those methods. In this example, the Assistant class burns documents by setting them on fire in a trash can, while the Executive class does so through somewhat more aggressive means (setting the executive's office on fire).