note 95587 added to language.oop5.interfaces
| From: | btjakachiraatgmaildotcom at osu1 dot php dot net | Date: | Mon, 11 Jan 2010 09:52:42 +0000 |
| Subject: | note 95587 added to language.oop5.interfaces | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164840@lists.php.net to get a copy of this message | ||
I'm going to give a very simple explanation between interface and any abstract. I'm not
going to repeat basic stuff mentioned above e.g you can not instantiate an interface or an abstract.
Any class with an abstract method should be declared as abstract.. etc.
Example.
I will show you where and when to use interfaces. Normally people will use interface when
absctracting becomes a problem. Lets say we start with the following objects
<?php
Person
Employee
Employer
Criminal
Rapist
President
Student
?>
You will easily see that Employee is a Person, Criminal is a person etc.. Therefore we can have
Person as a PARENT class. For person to be abstract, you decide within your application if a person
object makes sense... if it doesn't make the person an abstract so that you wont have alien
objects.
Now, interfaces are used when, in a group of objects, you have two or more objects that share
similar behaviour. E.g President and Employer will makePolicy() while Criminal and Rapist will
commitCrime(). Having said that, normally people would put these methods in their respective classes
(but defeating the OO designs). If you put makePolicy() in President and Employer class there will
be duplication of code. Other people can be tempted to put thet makePolicy() and commitCrime() in
Person as abstract methods so that the 6 objects will see the methods. Its not a good idea as
Student or Employee will not normally makePolicy() or commitCrime(). In fact, it means Employee
object can makePolicy() of increasind salary :-) . Therefore in this case we use INTERFACES
<?php
interface HighOffice {
public function makePolicy();
public function declareEmergency();
}
interface jailable {
public function commitCrime();
public function appeal();
}
abstract class Person {
public function getAge()
{
return "28years";
}
}
public class Employee extend Person {
//do stuff for employee
}
public class Criminal extend Person impliments jailable{
//do stuff for a criminal.
//these must be present.
public function commitCrime()
{
}
public function appeal()
{
}
public class President extend Person impliments HighOffice
{
//do other stuff for President
//then a President should make policies and declare emergency
public function makePolicy()
{
}
public function declareEmergency()
{
}
}
}
?>
NB: forgive me for errors syntax. I in the middle of doing Java.
You have noticed that relevant behavious have been added to the relevant objects..
----
Server IP: 69.147.83.197
Probable Submitter: 196.211.45.34
----
Manual Page -- http://www.php.net/manual/en/language.oop5.interfaces.php
Edit -- https://master.php.net/note/edit/95587
Del: integrated -- https://master.php.net/note/delete/95587/integrated
Del: useless -- https://master.php.net/note/delete/95587/useless
Del: bad code -- https://master.php.net/note/delete/95587/bad+code
Del: spam -- https://master.php.net/note/delete/95587/spam
Del: non-english -- https://master.php.net/note/delete/95587/non-english
Del: in docs -- https://master.php.net/note/delete/95587/in+docs
Del: other reasons-- https://master.php.net/note/delete/95587
Reject -- https://master.php.net/note/reject/95587
Search -- https://master.php.net/manage/user-notes.php