note 103118 added to language.oop5.abstract
| From: | a dot tsiaparas at watergate dot gr | Date: | Sat, 26 Mar 2011 23:57:02 +0000 |
| Subject: | note 103118 added to language.oop5.abstract | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178242@lists.php.net to get a copy of this message | ||
Abstraction and interfaces are two very different tools. The are as close as hammers and drills.
Abstract classes may have implemented methods, whereas interfaces have no implementation in
themselves.
Abstract classes that declare all their methods as abstract are not interfaces with different names.
One can implement multiple interfaces, but not extend multiple classes (or abstract classes).
The use of abstraction vs interfaces is problem specific and the choice is made during the design of
software, not its implementation. In the same project you may as well offer an interface and a base
(probably abstract) class as a reference that implements the interface. Why would you do that?
Let us assume that we want to build a system that calls different services, which in turn have
actions. Normally, we could offer a method called execute that accepts the name of the action as a
parameter and executes the action.
We want to make sure that classes can actually define their own ways of executing actions. So we
create an interface IService that has the execute method. Well, in most of your cases, you will be
copying and pasting the exact same code for execute.
We can create a reference implemention for a class named Service and implement the execute method.
So, no more copying and pasting for your other classes! But what if you want to extend MySLLi?? You
can implement the interface (copy-paste probably), and there you are, again with a service.
Abstraction can be included in the class for initialisation code, which cannot be predefined for
every class that you will write.
Hope this is not too mind-boggling and helps someone. Cheers,
Alexios Tsiaparas
----
Server IP: 69.147.83.197
Probable Submitter: 84.254.32.160
----
Manual Page -- http://www.php.net/manual/en/language.oop5.abstract.php
Edit -- https://master.php.net/note/edit/103118
Del: integrated -- https://master.php.net/note/delete/103118/integrated
Del: useless -- https://master.php.net/note/delete/103118/useless
Del: bad code -- https://master.php.net/note/delete/103118/bad+code
Del: spam -- https://master.php.net/note/delete/103118/spam
Del: non-english -- https://master.php.net/note/delete/103118/non-english
Del: in docs -- https://master.php.net/note/delete/103118/in+docs
Del: other reasons-- https://master.php.net/note/delete/103118
Reject -- https://master.php.net/note/reject/103118
Search -- https://master.php.net/manage/user-notes.php