inc.php.
208 CHAPTER 7 ?– ADVANCED OOP FEATURES
Abstract classes ensure conformity because any classes derived from them must
implement all abstract methods derived within the class. Attempting to forgo implementation
of any abstract method defined in the class results in a fatal error.
ABSTRACT CLASS OR INTERFACE?
When should you use an interface instead of an abstract class, and vice versa? This can be quite
confusing and is often a matter of considerable debate. However, there are a few factors that can help
you formulate a decision in this regard:
??? If you intend to create a model that will be assumed by a number of closely related objects,
use an abstract class. If you intend to create functionality that will subsequently be embraced
by a number of unrelated objects, use an interface.
??? If your object must inherit behavior from a number of sources, use an interface. PHP classes can
inherit multiple interfaces but cannot extend multiple abstract classes.
??? If you know that all classes will share a common behavior implementation, use an abstract class
and implement the behavior there. You cannot implement behavior in an interface.
Introducing Namespaces
As your class libraries continue to grow, you??™ll likely eventually encounter a situation
where two libraries use identical class names. However, historically, it hasn??™t been
possible to use two identically named classes in the same PHP script due to the requirement
that each class have a unique name.
Pages:
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286