Thursday, January 5, 2017

Interfaces – PHP OOP Tutorial

courtasy : http://jaskokoyn.com/2013/06/11/interfaces/


In this tutorial, you’ll learn how to create interfaces and why you should use them. They are very similar to abstract classes. At the end of this tutorial, we’ll discuss the differences and the benefits of each one.
We’ll be creating something similar to our recipe and cookie class in our abstraction tutorial.

Creating Interfaces

Create a file called index.php and add this bit of code.
Create a folder called interfaces and then create the recipe.interface.php file and cookie.interface.php file.  Let’s first create the Recipe interface.
Here’s what our Recipe interface looks like.
To create an interface you use the interface keyword. This keyword tells PHP that you’re creating an interface. An interface can not contain properties, but it can contain constants and methods. Any method you create in the interface can not contain any concrete code inside of it. All methods created must be public. You’ll also notice we don’t have to repeat the interface keyword before every method.
Now, that we’ve created an interface. Let’s create the cookie class.
Inside the cookie.interface.php file, add this bit of code.
You’ll notice that we’re not using the keyword extends to use the interface. Instead, you have to use the implements keyword in order to use an interface. Just like abstract classes, you must define any methods defined in the interface in your class.
Just like abstract classes, you CAN NOT instantiate interfaces. So, in our index.php file, we instantiate the cookie class. This is how you create interfaces.
Interfaces can be useful as they can be used as an empty shell or template for classes that need logic structured for them.

Interfaces vs Abstract Classes

Interfaces and abstract classes are pretty similar. They both help create templates for classes that need them. So, which one do you use and what are the major differences? Here a couple of things to know about each one.
  1. Methods in abstract classes can be defined abstract or just a regular method. If a method is abstract, then that method must be defined in the class that extends it. In interfaces,ALL methods are abstract, therefore all methods must be defined in the class that implements them.
  2. You’re allowed to define your methods as public, private, or protected in abstract classes. In interfaces, ALL methods MUST be public.
  3. In abstract classes, you’re allowed to create methods and properties with values and concrete code in them as long as they’re not defined as abstract. Interfaces CAN NOT contain properties and methods CAN NOT have a body.
  4. A class can only extend 1 abstract class, but a class can implement an infinite amount of interfaces even if the interfaces don’t relate to one another.
  5. You have the option to override methods in an abstract class, but you MUST override ALL methods in an interface.
As I stated earlier, you can have a class inherit multiple interfaces. To do this, you separate each interface with a comma. Let’s say I had an interface named business. Here’s how our cookie class would implement both the recipe and business interface.

Conclusion

As you can see, interfaces are a bit more stricter than abstract classes. Interfaces are meant to be used  just as a template or empty shell for other classes to follow a certain logic. Abstract classes are lenient and can contain some helpful methods and properties for any class that extends it. It’s up to you to choose which one best suits your project. In most cases, you won’t find yourself using interfaces or abstract classes often. For more information on interfaces, then go here.

No comments:

Post a Comment