When & Why
Interfaces, Abstracts, Traits
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.
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.
Built-In Interfaces
Traversable
Iterator
IteratorAggregate
Throwable
ArrayAccess
Serializable
Closure
Generator
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.
Trait
Cannot type hint for trait
Can be used to fulfill and interface
Can include more then one
Consistency, DRY
Tip
Interface
Think “able”
May be implemented in different ways
Inheritance
Think “is-a”
Many things work the same way
Trait
Think “has-a”
Extra Patterns
Singletons
Factories
Collections
Dependency Injection
Alena Holligan
• Wife, and Mother of 3 young children
• PHP Teacher at Treehouse
• Portland PHP User Group Leader
• Cascadia PHP Conference (cascadiaphp.com)
@alenaholligan alena@holligan.us https://joind.in/talk/0cf82

When & Why: Interfaces, abstract classes, traits

  • 1.
    When & Why Interfaces,Abstracts, Traits
  • 2.
    Interfaces: Primary Purposes Interfacesallow 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.
    Interface: When touse 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.
  • 5.
    Abstract Class: Whento 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.
    Trait Cannot type hintfor trait Can be used to fulfill and interface Can include more then one Consistency, DRY
  • 7.
    Tip Interface Think “able” May beimplemented in different ways Inheritance Think “is-a” Many things work the same way Trait Think “has-a”
  • 8.
  • 9.
    Alena Holligan • Wife,and Mother of 3 young children • PHP Teacher at Treehouse • Portland PHP User Group Leader • Cascadia PHP Conference (cascadiaphp.com) @alenaholligan alena@holligan.us https://joind.in/talk/0cf82