PHP Interfaces, the forgotten feature!



It’s been a while since I’ve posted anything but I thought I would write something “coding wise” beneficial.. (you know, because I never talk about anything “important”)

So today I thought I might provide an example that hasn’t (in my opinion) been that well explained on the internet, this my friends is PHP-OOPs lost feature “interfaces”

(Note: not so much as “lost” feature but lets face it, every semi php programmer knows basic OOP but they may not know about things like abstraction or interfaces, it’s very much a hidden feature)

So as an aperture science youngster you may ask:

“What is an interface in PHP-OOP?”

Well, think of an interface as a “contract” with one or more classes!

(Note: when I state contract, think of like a loan shark… an interface will break your applications legs unless you comply exactly to it’s minimal requirements)

Interfaces act as a way to ensure that one or more classes has certain public, private and protected methods and/or variables, it’s used to define a common implementation within your class.

Why is this important?  well the best example I could think of is a language class… there’s not a huge amount of things I could come up with that had the simplicity this does, so here we go!

Interfaces ensure you don’t leave any important methods out, thus limiting the amount of heart attacks your web application could have!

(The irony of this is that interfaces are designed to limit the heart attacks your webapp by threatening you with a heart attack)

Without further a due, lets break out the code!

So, for this example lets say we built our own website in PHP, now as we’re a multizillion dollar company, we have clients globally, even on the space station! it’s important to implement culture classes (english / french / voltron) language support.

So this is where “interfaces” might come in handy, lets say we have a class culture “enUS”

class enUS extends languageCore
{
   public function returnAboutUStext() { return "This is my about us text"; }
   public function returnPageHometext() { return "paragraph one on homepage text"; }
}

Now for the simplicity of this, lets say that there’s like 100 more methods to implement..

At this point, nearly all PHP elitists would have me slaughtered for implementing a language file this way, but this is not the real deal my friends, just an example!

But what happens if you start having translators who want to write translation classes? this is where interfaces come in!

As stated before, interfaces are like a contract, you can’t run unless all consignments have been fulfilled… so how do we do this?

Easy because interface files contain no “working” code!

See this example for the above class:

interface textImplemented {
     public function returnAboutUStext();
     public function returnPageHometext();
}

Notice  how an interface file has no working code? just whether its private, public or protected and it’s name, easy easy!

so we can now implement this with:

class enUS extends languageCore implements textImplemented {
/*code removed*/
}

so unless class enUS contains the methods listed in the interface WITH their appropriate security level and conditions, it will fail with something similar to the following:

 PHP Fatal error:  Class enUS contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (textImplementation::returnAboutUStext, textImplemented::returnPageHomeText)

What does this mean? well it means that you can specify all the classes methods that need to be implemented in multiple classes in order for systems to function properly!

Which allows us to then not need to make any code changes to the core application, so you end up having the same method names contracted (keyword: contracted) into the classes for each language.

This is a basic example of how an interface can be implemented.

 

Just to stir the pot and end on a high note, you can implement multiple interfaces to a class as well, example:

class enUS extends languageCore implements textImplemented, textRespresented, textVariables { /* class code */ }

So, now you know about it; start using it!

 

Enjoy!

Cheers,

–Karl.