blacksmoke90 Posted May 2, 2015 Share Posted May 2, 2015 Good evening I hope someone would be so kind as to help me. I am completely new too php oop and I just need help with one tiny problem. I have written some code and I understand most of the concepts concerning oop in php, it's just that I am struggling with interfaces and I don't really see why it should be used if I can just create methods, but anyway I was told it is a good practice to use interfaces because one of the main reasons of oop is to re-use code, So I have implemented an interface but I can call the method with and without the implementation of the interface so I was kind of wondering, what the purpose of interfaces are? or am I doing something wrong? Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 3, 2015 Share Posted May 3, 2015 An interface is typically used by framework and library creators. It facilitates certain types of design patterns, for example the factory pattern or for use in dependency injection situations. What an interface guarantees is that *your* class will be sure to include some number of methods that another class can call. For example, let's say that I have a "shapeMaker" class that instantiates a graphic canvas and then adds and removes objects of different shapes to it. It might have a method like this: class ShapeMaker($canvas) { private $shapes = array(); private $canvas; ..... function addShape($shapeObj) { $this->shapes[] = $shapeObj; $shapeObj->draw($this->canvas); } } The designer of the framework will very probably include an interface for people to create new shape classes, which include the draw($shapeObj) method signature. By providing that interface, they guarantee that any object that is passed into the shapeMaker class via the addShape, can then immediately have it's draw($canvas) method called. The details of exactly how draw works are left to the class developer, but the interface helps establish the contract of methods needed by the factory class in order to manipulate any object that implements an interface. I rarely see the use of interfaces outside of a framework or library, but there is no reason you can't use them yourself if you have a solid grasp of what they are for. Usually people are utilizing inheritance rather than interfaces, but in complicated class dependency situations, it's good to keep in mind that you can write a class that can implement 2 or 3 different interfaces, and still inherit from a base class. In PHP, you only have single inheritance, so interfaces were a way to get around that limitation in some circumstances, although in php5.4 they added traits which are another way of getting around single inheritance in many cases. Quote Link to comment Share on other sites More sharing options...
maxxd Posted May 4, 2015 Share Posted May 4, 2015 Another benefit to using interfaces is that you can type hint to the interface, like so: interface iMyInterface{ function sayHello(); } class TestOne implements iMyInterface{ function sayHello(){ return '<p>Good day!</p>'; } } class testTwo implements iMyInterface{ function sayHello(){ return "<p>'sup?</p>"; } } class EverybodyPolite{ public static function greet(iMyInterface $greeting){ print($greeting->sayHello()); } } $p1 = new TestOne(); $p2 = new TestTwo(); EverybodyPolite::greet($p1); EverybodyPolite::greet($p2); While you can drop the type hint from the public static greet() method, having it there increases readability of the code and the possibilities that the code will function as expected because you know that the object passed in to the calling method functions (at it's base) in a defined way. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.