Interfaces
& Abstract Classes
Yet another new feature added to PHP
5 is that of Interfaces and Abstract classes. These are both used to create a
series of constraints on the base design of a group of classes. An abstract
class essentially defines the basic skeleton of a specific type of encapsulated
entity—for example, you can use an abstract class to define the basic concept of
“car” as having two doors, a lock and a method that locks or unlocks the doors.
Abstract classes cannot be used directly, but they must be extended so that the
descendent class provides a full complement of methods. For example:
abstract class
DataStore_Adapter {
private $id;
abstract function insert();
abstract function update();
public function save()
{
if (!is_null($this->id)) {
$this->update();
} else {
$this->insert();
}
}
}
class PDO_DataStore_Adapter
extends DataStore_Adapter {
public__construct($dsn)
{
// ...
}
function insert()
{
// ...
}
function update()
{
// ...
}
}
As you can see, in this example we
define a class called DataStore_Adapter and declare two abstract methods called
insert() and update(). Note how these methods don’t actually have a body—that’s
one of the requirements of abstract classes—and how the class itself must be
declared as abstract in order for the compiler to satisfy the parser’s
syntactic requirements. We then extend DataStore_Adapter into PDO_DataStore_Adapter,
which is no longer abstract because we have now provided a body for both
insert() and update().
Interfaces
Interfaces, on the other hand, are
used to specify an API that a class must implement. This al lows you to create
a common “contract” that your classes must implement in order to satisfy certain
logical requirements—for example, you could use interfaces to abstract the
concept of database provider into a common API that could then be implemented
by a series of classes that interface to different DBMSs.
Interface methods contain no body:
interface DataStore_Adapter
{
public function insert();
public function update();
public function save();
public function newRecord($name = null);
}
class PDO_DataStore_Adapter
implements DataStore_Adapter {
public function insert()
{
// ...
}
public function update()
{
// ...
}
public function save()
{
// ...
}
public function newRecord($name = null)
{
}
}
If, in the example above, you fail
to define all of the methods for a particular interface, or all of the arguments
for any given interface method, you will see something like this:
Fatal error: Class PDO_DataStore_Adapter contains 1 abstract
method and must therefore be declared abstract or implement the remaining
methods (DataStore_Adapter::save) in //document// on line 27
Or
Fatal error: Declaration of PDO_DataStore_Adapter::newRecord()
must be compatible with that of DataStore_Adapter::newRecord() in //document//
on line 12
It is also possible to implement
more than one i nterface in the same class:
class PDO_DataStore_Adapter implements DataStore_Adapter,
SeekableIterator {
// ...
}
In this example, we need to define
the methods for both DataStore_Adapter and SeekableIterator. Additionally, a
class can extend another class, as well as implement multiple interfaces at the
same time:
class PDO_DataStore_Adapter extends
PDO implements
DataStore_Adapter, SeekableIterator
{
// ...
}
Determining
An Object’s Class
It is often convenient to be able to
determine whether a given object is an instance of a particular class, or
whether it implements a specific interface. This can be done by using the
instanceof operator:
if ($obj instanceof MyClass) {
echo "\$obj is an instance of
MyClass";
}
Naturally, instanceof allows you to
inspect all of the ancestor classes of your object, as well as any interfaces.
0 Comment to " PHP Interfaces & Abstract Classes "
Post a Comment