note 107748 added to language.oop5.interfaces
| From: | anon | Date: | Thu, 01 Mar 2012 15:13:41 +0000 |
| Subject: | note 107748 added to language.oop5.interfaces | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186449@lists.php.net to get a copy of this message | ||
dlovell2001's example does not quite get it right either. Both interfaces and classes provide
abstraction. His example would be better implemented as an abstract class than an interface.
The distinction between the two is that an abstract class says what an object is and describes its
visible appearance. An interface describes the visible appearance without restricting what the
object may be.
Whether the Database is an SQL database, an Oracle database, or some other type of database, it is
still a database - a particular kind of object. In this case, the description should be an abstract
class - class because it is a database, abstract because the class is useless without knowing just
what kind of database it is.
On the other hand, consider the iterator. The object may be an array, a json string, a filesystem
directory tree, a database result set, the infinite set of prime numbers, a random number generator,
etc. While these are all different objects, they share in common the fact that they have a first
value and that each value will be followed by another until a last value is reached, if ever. These
are all different types of objects, so different classes, but share a common way of accessing these
values. This is where you use an interface.
Use abstract classes to declare the common method signatures of related classes. Use interfaces to
declare the common method signatures of unrelated classes.
And while we are here, once you have 5.4, use parent classes to define the common methods and
properties of related classes and traits to define the common methods and properties of unrelated
classes.
----
Server IP: 192.41.42.26
Probable Submitter: 75.132.154.20
----
Manual Page -- http://www.php.net/manual/en/language.oop5.interfaces.php
Edit -- https://master.php.net/note/edit/107748
Del: integrated -- https://master.php.net/note/delete/107748/integrated
Del: useless -- https://master.php.net/note/delete/107748/useless
Del: bad code -- https://master.php.net/note/delete/107748/bad+code
Del: spam -- https://master.php.net/note/delete/107748/spam
Del: non-english -- https://master.php.net/note/delete/107748/non-english
Del: in docs -- https://master.php.net/note/delete/107748/in+docs
Del: other reasons-- https://master.php.net/note/delete/107748
Reject -- https://master.php.net/note/reject/107748
Search -- https://master.php.net/manage/user-notes.php