News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Josh Adell:
Interfaces and Traits A Powerful Combo
September 28, 2012 @ 08:51:16

Josh Adell has a new post today looking at the "powerful combination" of using traits and interfaces in PHP applications. He shows how, despite traits not implementing the interface directly, they can be used to make other classes adhere to them simply by "using" them.

If you're not using interfaces in PHP, you are missing out on a powerful object-oriented programming feature. An interface defines how to interact with a class. By defining an interface and then implementing it, you can guarantee a "contract" for consumers of a class. Interfaces can be used across unrelated classes. And they become even more useful when combined with the new traits feature in PHP 5.4.

He illustrates with a package shipping example and uses an "Addressable" Interface to define the structure for both a Company and Users class. He includes code showing how to implement it in a more traditional "implements" way in a class, but also shows an interesting way to achieve the same thing with traits. Having a trait that follows the interface makes it easy to have a class adhere to the interface just by including the trait (or "using" it).

0 comments voice your opinion now!
interface trait tutorial implement use structure


blog comments powered by Disqus

Similar Posts

Robert Basic's Blog: Using the new autoloaders from Zend Framework 1.12

Fabien Potencier's Blog: Create your own framework... on top of the Symfony2 Components (part 11)

Josh Adell: Interfaces and Traits: A Powerful Combo

Joey Rivera's Blog: Caching using PHP/Zend_Cache and MySQL

Ruben Vermeersch's Blog: Integrating Zend Framework and Doctrine


Community Events





Don't see your event here?
Let us know!


release developer language install refactor community code interview series list configure introduction unittest framework laravel threedevsandamaybe podcast opinion wordpress testing

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework