Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

When & Why: Interfaces, abstract classes, traits


Published on

Naming things is hard but sorting out how your application should be structured shouldn’t be! It’s always frustrating when the boss or client hands you this giant ball of requirements and how everything is interconnected. Half the fun of programming is breaking these down and turning these requirements into code to solve the business needs. We’ll cover several code scenarios and show you the best way to break down complex domains.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

When & Why: Interfaces, abstract classes, traits

  1. 1. When & Why Interfaces, Abstracts, Traits
  2. 2. Interfaces: Primary Purposes Interfaces allow you to define/create a common structure for your classes – to set a standard for objects. Interfaces solves the problem of single inheritance – they allow you to inject ‘qualities’ from multiple sources. Interfaces provide a flexible base/root structure that you don’t get with classes. Interfaces are great when you have multiple coders working on a project – you can set up a loose structure for programmers to follow and let handle details.
  3. 3. Interface: When to use If you have a class that is never directly instantiated in your program, this is a good candidate for an interface. In other words, if you are creating a class to only serve as the parent to other classes, it should probably be made into an interface. When you know what methods a class should have but you are not sure what the details will be. When you want to quickly map out the basic structures of your classes to serve as a template for others to follow – keeps the code-base predictable and consistent.
  4. 4. Built-In Interfaces Traversable Iterator IteratorAggregate Throwable ArrayAccess Serializable Closure Generator
  5. 5. Abstract Class: When to use You want to share code among several closely related classes. You expect that classes that extend your abstract class have many common methods or fields or require access modifiers other than public (such as protected and private). You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
  6. 6. Trait Cannot type hint for trait Can be used to fulfill and interface Can include more then one Consistency, DRY
  7. 7. Tip Interface Think “able” May be implemented in different ways Inheritance Think “is-a” Many things work the same way Trait Think “has-a”
  8. 8. Extra Patterns Singletons Factories Collections Dependency Injection
  9. 9. Alena Holligan • Wife, and Mother of 3 young children • PHP Teacher at Treehouse • Portland PHP User Group Leader • Cascadia PHP Conference ( @alenaholligan