note 107364 added to language.oop5.interfaces
| From: | dlovell2001 at yahoo dot com | Date: | Fri, 03 Feb 2012 22:41:01 +0000 |
| Subject: | note 107364 added to language.oop5.interfaces | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185843@lists.php.net to get a copy of this message | ||
It seems like many contributors are missing the point of using an INTERFACE. An INTERFACE is not
specifically provided for abstraction. That's what a CLASS is used for. Most examples in this
article of interfaces could be achieved just as easily using just classes alone.
An INTERFACE is provided so you can describe a set of functions and then hide the final
implementation of those functions in an implementing class. This allows you to change the
IMPLEMENTATION of those functions without changing how you use it.
For example: I have a database. I want to write a class that accesses the data in my database. I
define an interface like this:
interface Database {
function listOrders();
function addOrder();
function removeOrder();
...
}
Then let's say we start out using a MySQL database. So we write a class to access the MySQL
database:
class MySqlDatabase implements Database {
function listOrders() {...
}
we write these methods as needed to get to the MySQL database tables. Then you can write your
controller to use the interface as such:
$database = new MySqlDatabase();
foreach ($database->listOrders() as $order) {
Then let's say we decide to migrate to an Oracle database. We could write another class to get
to the Oracle database as such:
class OracleDatabase implements Database {
public function listOrders() {...
}
Then - to switch our application to use the Oracle database instead of the MySQL database we only
have to change ONE LINE of code:
$database = new OracleDatabase();
all other lines of code, such as:
foreach ($database->listOrders() as $order) {
will remain unchanged. The point is - the INTERFACE describes the methods that we need to access our
database. It does NOT describe in any way HOW we achieve that. That's what the IMPLEMENTing
class does. We can IMPLEMENT this interface as many times as we need in as many different ways as we
need. We can then switch between implementations of the interface without impact to our code because
the interface defines how we will use it regardless of how it actually works.
----
Server IP: 69.147.83.197
Probable Submitter: 63.88.66.129
----
Manual Page -- http://www.php.net/manual/en/language.oop5.interfaces.php
Edit -- https://master.php.net/note/edit/107364
Del: integrated -- https://master.php.net/note/delete/107364/integrated
Del: useless -- https://master.php.net/note/delete/107364/useless
Del: bad code -- https://master.php.net/note/delete/107364/bad+code
Del: spam -- https://master.php.net/note/delete/107364/spam
Del: non-english -- https://master.php.net/note/delete/107364/non-english
Del: in docs -- https://master.php.net/note/delete/107364/in+docs
Del: other reasons-- https://master.php.net/note/delete/107364
Reject -- https://master.php.net/note/reject/107364
Search -- https://master.php.net/manage/user-notes.php