This is achieved using interfaces.
Licensed to Menshu You
Zend Framework in Action (Ch01) Manning Publications Co. 53
Interfaces
Interfaces are a mechanism for defining a template for classes that implement the interface, that is, an API or
contract that the class satisfies. In practical terms, this means that an interface is a list of functions that must be
defined by implementing classes. Interfaces are used to allow a class to be used by a function that is depending
upon the class having certain functionality implemented. If we were to make ZFiA_Person an interface, it
would be declared as shown in listing 17.7.
Listing 17.7 An interface is a class template defining which functions must be defined.
interface ZFiA_Person
{
public function fullName();
}
class ZFiA_Author implements ZFiA_Person
{
protected $_firstName; A
protected $_lastName; A
public __construct($firstName, $lastName) B
{
$this->_firstName = $firstName; C
$this->_lastName = $lastName; C
}
public fullName() D
{ D
return $this->_firstName . ' ' . $this->_lastName; D
} D
}
If the fullName method is not defined in ZFiA_Author it will result in a fatal error. As PHP is not a statically
typed language, Interfaces are a convenience mechanism for the programmer to help prevent logical errors.
Pages:
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375