Talk from NEPHP 2015
Code can be found at https://github.com/chasepeeler/nephp2015. Each slide that references looking at code corresponds to a different branch, p0 - p3.
Interfaces and traits go together like peas and carrots
1. Interfaces and Traits Go Together Like
Peas and Carrots
Chase Peeler - Northeast PHP - 8/23/2015
Chase Peeler
Northeast PHP
August 23, 2015
2. Agenda
• Introductions
• Bubba Gump restaurant
• Interfaces
• Traits
• But what about …?
• Questions
Chase Peeler - Northeast PHP - 8/23/2015
3. Introductions
• Started with PHP in 1999
• Used professionally since 2005
• Currently Senior Software Architect
I may not be a smart man, but I know what PHP is
Chase Peeler - Northeast PHP - 8/23/2015
4. Bubba Gump Restaurant
• No affiliation with “Bubba Gump Shrimp Company”
• System for preparing, cooking, and serving different types of shrimp
Chase Peeler - Northeast PHP - 8/23/2015
5. Time to look at code!
Chase Peeler - Northeast PHP - 8/23/2015
6. What was wrong?
Chase Peeler - Northeast PHP - 8/23/2015
• Not easy to scale
• Lots of duplicated code
7. Interfaces
• “Object interfaces allow you to create code which specifies which
methods a class must implement, without having to define how these
methods are handled.” (http://php.net/manual/en/language.oop5.interfaces.php)
• You can only define public methods
• Cannot provide any functionality
• Allows your code to treat ALL classes that implement the interface as
if they were the same
Chase Peeler - Northeast PHP - 8/23/2015
8. Time to look at more code!
Chase Peeler - Northeast PHP - 8/23/2015
9. Better, but not great
• Still a lot of repeated code
• Copy/paste is bad
Chase Peeler - Northeast PHP - 8/23/2015
10. Traits
• “Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling
a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and
classes is defined in a way which reduces complexity, and avoids the typical problems associated with multiple inheritance and Mixins. A Trait is similar to a class,
but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional
inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring
inheritance.”(http://php.net/manual/en/language.oop5.traits.php)
• Compiler assisted copy/paste
• Cannot be instantiated on their own
• Shared Behaviors
Chase Peeler - Northeast PHP - 8/23/2015
11. To the code again!
Chase Peeler - Northeast PHP - 8/23/2015
12. What about abstract classes?
• Example was a bit contrived
• What if you are already inheriting from a parent class?
• Does all of that functionality really belong in a parent class?
• Hierarchy can get really complex, really fast.
Chase Peeler - Northeast PHP - 8/23/2015
13. Let’s look at the code, one more time
Chase Peeler - Northeast PHP - 8/23/2015
14. Summary
• Copy/paste is bad unless the compiler does it
• Traits can provide ways of defining default functionality for your interfaces
• Traits prevent the need for parent classes that are able to do too many
different things
Chase Peeler - Northeast PHP - 8/23/2015
----- Meeting Notes (8/18/15 11:16) -----
Freshman year at GT - didn't learn it in any classes, but used it in some 3/4000 level course
Graduated 2005, went to work for Endeavor
Endeavor is not a software company
Has internal webapp built in PHP - was 2-3 years old when I started
Lot of spaghetti code.. I made it worse
Knew it was wrong, but didn't know how to make it right
Now, SSA - built a new framework from the ground up
Just released a new interface, rest API backend, backbone+marionette frontend
----- Meeting Notes (8/18/15 11:20) -----
First time speaking - go easy on me
----- Meeting Notes (8/18/15 11:20) -----
Somewhat contrived, but fits within our theme and should make sense
----- Meeting Notes (8/18/15 11:20) -----
Pause for feedback
----- Meeting Notes (8/18/15 11:20) -----
Can be treated as a "type"
----- Meeting Notes (8/18/15 11:20) -----
Works very well when sharing common functionality across the application, even between unrelated modules and classes