First, however, take a moment to
understand how PHP 5 implements interfaces. In PHP, an interface is created like so:
interface IinterfaceName
{
CONST 1;
...
CONST N;
function methodName1();
...
function methodNameN();
}
?– Tip It??™s common practice to preface the names of interfaces with the letter I to make them easier
to recognize.
The contract is completed when a class implements the interface via the implements
keyword. All methods must be implemented, or the implementing class must be
declared abstract (a concept introduced in the next section); otherwise, an error
similar to the following will occur:
Fatal error: Class Executive contains 1 abstract methods and must
therefore be declared abstract (pillageCompany::emptyBankAccount) in
/www/htdocs/pmnp/7/executive.php on line 30
The following is the general syntax for implementing the preceding interface:
CHAPTER 7 ?– ADVANCED OOP FEATURES 205
class Class_Name implements interfaceName
{
function methodName1()
{
// methodName1() implementation
}
function methodNameN()
{
// methodName1() implementation
}
}
Implementing a Single Interface
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.
Pages:
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283