PHP - How Often Do You Use Interfaces?
So I have been writing PHP constantly for a few months now, and have a pretty good grasp on OOP coming from other languages. One thing I never really use is interfaces, I get it when a class implements an interface, that class is required to have implemented methods from a interface, or a constant that is required by an interface. Seems like the kinda thing you would need when you are on a large team mainly for naming consistency? Is this something I should be using? I know I can get away with not using it, but should I use it to be up to modern php standards?
Thanks
Similar TutorialsWhat is the purpose of using an Interface in OOP? I understand the textbook definition, but am not getting the extra value that they provide... TomTees Would someone help me out with Abstract Classes and Interfaces... Some questions... 1.) When you have an Interface with Methods, then any Concrete Class that "implements" said Interface must include all of the listed Methods, correct? 2.) When you have an Abstract Class with Properties & Methods, are you required to use all of the listed Properties & Methods similar to how an Interface works? 3.) What is the intent of using Abstract Classes vs. Interfaces? TomTees I am working on a module that receives some info and provides dimensions and weights of products to the caller.
The way I get those is from existing Excel tables used by the company. Some product types have their dimensions only. Some have both dimensions and weights. To clarify - all products have both weight and dimensions but not all are specified on the Excel tables that I am using. so say "Apple" has dimensions on 1 x 1 x 1, but no weight is given. (Weight can be computed separately and in fact there is an existing class that does this already for Pear.). and "Pear" has dimensions 2 x 1 x 1 and weighs 1lbs. In short, some classes have both getWeight() and getDimensions() methods, like Pear(), and some classes just have getDimensions(), like Apple(). I am confused as to which way to go. I can create PearDimensions() class and PearWeight() and separate concepts of "weight" and "dimensions" some more and use a DimensionFactory() and WeightFactory() to instantiate the right object for the circumstances, or I can use "implements" WeightAwaretInterface and HeightAwaretInterface on Apple/Pear classes and leave them as is, thus mixing up the Weight/Dimension getters/causes. So in short I am a tad confused and wondering if there is just a good way to maintain these. Right now I am using DimensionFactory() and WeightFactory() and instantiate Pear class twice (once in each factory) since Pear has both getWeight() and getDimensions() methods. I feel weird creating the same Pear object in both factories. Do you have a recommendation for this, or do I just "hack it" (i.e. leave it as is)? Hi, I am trying to get my head around classes, interfaces and methods for different animals
this is what I have so far: <?php interface animal { function walk(); function fly(); function swim(); } class monkey implements animal{ const interface1 = "I am from test class"; function noise() { echo "ooh ooh ah ah!"; } function walk() { echo "monkey is walking"; } function fly() { echo "monkey is Flying"; } function swim() { echo "monkey is Swimming"; } public function display() { echo monkey::interface1; echo PHP_EOL; } } class bear implements animal{ const interface1 = "I am from test class"; function noise() { echo "Grrr!"; } function walk() { echo "Bear is walking"; } function fly() { echo "Bear is Flying"; } function swim() { echo "Bear is Swimming"; } public function display() { echo bear::interface1; echo PHP_EOL; } } $Obj = new monkey(); $Obj->display(); $Obj1 = new bear(); $Obj1->display(); ?> I'm a bit stumped as animal interface not calling all functions?! please help Do interfaces in PHP include default methods, which are available in Java 7 and 8. Default methods allow you to add default functionality in the interface itself. So when a class implements it, it can use the default definition from the interface. This is in contrast to an abstract method which has no method body.
|