Please be patient with me on this one...
I just picked up the Wrox Professional PHP5 book so I could learn OOP. One thing that is confusing me is the need for interfaces (page 24 of the book if you happen to have it).
An interface enables you to specify that an object is capable of performing a certain function... An interface is a contract between unrelated objects to performa common function.
I understand that the interface basically makes sure that the class has certain specific methods, though I'm not sure why one would even use this feature. It seems like unnecessary work. If you want a method in a class, why not just implement it because you need it rather than saying some place else that you have those methods?
Thanks!Let's say we have an interface which requires any class which implements it to have a method called requiredMethod(). We would then be able to use the following code on an array of unspecified objects without risk of error.
foreach($objectlist as $object) {
if($object instanceof 'myInterface') {
$object->requiredMethod();
}
}
This is a simple example of their use but I think it shows the concept quite well. Basically, when you say that a class implements an interface you are garunteeing that it complies to a particular, well, interface. This allows you to pass the object in on type-hinted parameter lists where that interface is required.Ah. that makes sense. Thanks!
It's hard learning this stuff when they throw so much at you at once; sometimes it's hard to see how these things really fit into the big picture (rather than just the example they're giving).As Bubblenut says coding to interfaces allows flexibility when working with different types of object which need a common API. A good example is the Iterator interface thats in the Standard PHP Library. If you make a class which implements Iterator then you must implement the interface's methods. However once you have done this you can treat any object which implements Iterator in the same way, since they must have a common API. So iterating over a directory, a database recordset, an array, or any sort of list can be handled by the same client code.
A real world example might be a search function, where you want to search a file system and a database. By using Iterator the code which lays up the search results doesn't need to know what it is displaying, just that it is_a Iterator.
Interfaces are also PHP's answer to multiple inheritance. Class Foo might implement interface Ifoo, where as class FooBar might implement interfaces Ifoo and Ibar.yeah... ive only just started playing around with these, and i hope im doing it right. im developing somewhat of a cms framework at the moment.... now, i want this framwork to be able to work with lots of different db's. so the first thing i made was a db interface. i have then made two classes (one for sqlite, and one for mysql) which both impliment this interface.
this way, if (when) i want to use another db all i have to do is build another class for that db that also impliments this same interface, and i know, it will work safely within my framework.
I just picked up the Wrox Professional PHP5 book so I could learn OOP. One thing that is confusing me is the need for interfaces (page 24 of the book if you happen to have it).
An interface enables you to specify that an object is capable of performing a certain function... An interface is a contract between unrelated objects to performa common function.
I understand that the interface basically makes sure that the class has certain specific methods, though I'm not sure why one would even use this feature. It seems like unnecessary work. If you want a method in a class, why not just implement it because you need it rather than saying some place else that you have those methods?
Thanks!Let's say we have an interface which requires any class which implements it to have a method called requiredMethod(). We would then be able to use the following code on an array of unspecified objects without risk of error.
foreach($objectlist as $object) {
if($object instanceof 'myInterface') {
$object->requiredMethod();
}
}
This is a simple example of their use but I think it shows the concept quite well. Basically, when you say that a class implements an interface you are garunteeing that it complies to a particular, well, interface. This allows you to pass the object in on type-hinted parameter lists where that interface is required.Ah. that makes sense. Thanks!
It's hard learning this stuff when they throw so much at you at once; sometimes it's hard to see how these things really fit into the big picture (rather than just the example they're giving).As Bubblenut says coding to interfaces allows flexibility when working with different types of object which need a common API. A good example is the Iterator interface thats in the Standard PHP Library. If you make a class which implements Iterator then you must implement the interface's methods. However once you have done this you can treat any object which implements Iterator in the same way, since they must have a common API. So iterating over a directory, a database recordset, an array, or any sort of list can be handled by the same client code.
A real world example might be a search function, where you want to search a file system and a database. By using Iterator the code which lays up the search results doesn't need to know what it is displaying, just that it is_a Iterator.
Interfaces are also PHP's answer to multiple inheritance. Class Foo might implement interface Ifoo, where as class FooBar might implement interfaces Ifoo and Ibar.yeah... ive only just started playing around with these, and i hope im doing it right. im developing somewhat of a cms framework at the moment.... now, i want this framwork to be able to work with lots of different db's. so the first thing i made was a db interface. i have then made two classes (one for sqlite, and one for mysql) which both impliment this interface.
this way, if (when) i want to use another db all i have to do is build another class for that db that also impliments this same interface, and i know, it will work safely within my framework.