Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Grab Bag of PHP Goodies

Justin Yost
September 02, 2016

Grab Bag of PHP Goodies

Random topics and tools from the PHP language.

Justin Yost

September 02, 2016
Tweet

More Decks by Justin Yost

Other Decks in Programming

Transcript

  1. Grab Bag of PHP Goodies Justin Yost Project Lead at

    Loadsys CC BY-NC 4.0 Justin Yost 1
  2. What are we learning? • Traits • Interfaces • Abstract

    Classes • Magic Methods • Singletons • Namespaces CC BY-NC 4.0 Justin Yost 2
  3. Why learn this stuff? • There is a better way

    • More tools in your toolbag • All solve specific problems • PHP moves along, welcome to the future CC BY-NC 4.0 Justin Yost 3
  4. Trait A trait is a set of methods that we

    can use to extend our classes. It's not a standard inheritance model, instead it permits horizontal code reuse. CC BY-NC 4.0 Justin Yost 4
  5. Interface An interface is an abstract type that contains no

    data or code, but defines behaviors as method signatures. This gives you the ability to define the public api for a class, without providing the implementation details. CC BY-NC 4.0 Justin Yost 7
  6. Interface Interfaces solve the problem of you need to ensure

    other coders meet some specific API, but you don't care about their implementation details of how they implement that same API. CC BY-NC 4.0 Justin Yost 8
  7. Interface Why not just ship a base class and provide

    that. Sometimes you need to provide the implementation separate from the API. CC BY-NC 4.0 Justin Yost 9
  8. PSR Interfaces • Logger Interface (PSR-3) • Caching Interface (PSR-6)

    • HTTP Message Interface (PSR-7) CC BY-NC 4.0 Justin Yost 10
  9. Interface An interface is a contract that says, I have

    these methods, that take these inputs and return this. You can implement this in any way and I'll respect it. CC BY-NC 4.0 Justin Yost 11
  10. Interface and Trait This is a great approach for shipping

    code to other developers that they might want to modify or update, you provide the implementation and the api independently. CC BY-NC 4.0 Justin Yost 13
  11. Abstract Class Abstract Class blends an interface with a trait.

    It provides a way to provide both the api and the implementation, without needing a full class that includes everything. CC BY-NC 4.0 Justin Yost 15
  12. Abstract Class vs Interface Abstract class is for you, Interface

    is for other developers. Abstract class provides an implementation that you typically don't want to ship to other developers but use internally. CC BY-NC 4.0 Justin Yost 18
  13. Magic Methods Methods that happen without you specifically calling them.

    They just magically happen, for you. CC BY-NC 4.0 Justin Yost 19
  14. Magic Methods • __construct and __destruct • __sleep and __wakeup

    • __invoke • __set and __get • and more CC BY-NC 4.0 Justin Yost 21
  15. Singleton A singleton is a class that can only every

    be instantiated once. Once we have an instance of the class as long as we are in the same PHP request, we do not create multiple instances of this class with different properties. CC BY-NC 4.0 Justin Yost 23
  16. Namespace Allows for easier import and use of external packages

    and libraries. CC BY-NC 4.0 Justin Yost 28
  17. Grab Bag Portion • Typehints • Composer • Exceptions •

    Closures • Iterators CC BY-NC 4.0 Justin Yost 30