Slide 1

Slide 1 text

Grab Bag of PHP Goodies Justin Yost Project Lead at Loadsys CC BY-NC 4.0 Justin Yost 1

Slide 2

Slide 2 text

What are we learning? • Traits • Interfaces • Abstract Classes • Magic Methods • Singletons • Namespaces CC BY-NC 4.0 Justin Yost 2

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

CC BY-NC 4.0 Justin Yost 5

Slide 6

Slide 6 text

Trait Code Example CC BY-NC 4.0 Justin Yost 6

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

PSR Interfaces • Logger Interface (PSR-3) • Caching Interface (PSR-6) • HTTP Message Interface (PSR-7) CC BY-NC 4.0 Justin Yost 10

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Interface Code Example CC BY-NC 4.0 Justin Yost 12

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Typehints Typehint to an interface, not a trait CC BY-NC 4.0 Justin Yost 14

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Abstract Class Fruit vs. Apple and Grape CC BY-NC 4.0 Justin Yost 16

Slide 17

Slide 17 text

Abstract Class Code Example CC BY-NC 4.0 Justin Yost 17

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Magic Methods Methods that happen without you specifically calling them. They just magically happen, for you. CC BY-NC 4.0 Justin Yost 19

Slide 20

Slide 20 text

Magic Methods PHP Magic methods start with a __ CC BY-NC 4.0 Justin Yost 20

Slide 21

Slide 21 text

Magic Methods • __construct and __destruct • __sleep and __wakeup • __invoke • __set and __get • and more CC BY-NC 4.0 Justin Yost 21

Slide 22

Slide 22 text

Magic Methods Code Example CC BY-NC 4.0 Justin Yost 22

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Singleton • Database Connection • Configuration • DI Libraries CC BY-NC 4.0 Justin Yost 24

Slide 25

Slide 25 text

Singleton Code Example CC BY-NC 4.0 Justin Yost 25

Slide 26

Slide 26 text

Namespace Everything in it's place and place for everything. CC BY-NC 4.0 Justin Yost 26

Slide 27

Slide 27 text

Namespace Multiple Classes with the same name. CC BY-NC 4.0 Justin Yost 27

Slide 28

Slide 28 text

Namespace Allows for easier import and use of external packages and libraries. CC BY-NC 4.0 Justin Yost 28

Slide 29

Slide 29 text

Namespace Code Example CC BY-NC 4.0 Justin Yost 29

Slide 30

Slide 30 text

Grab Bag Portion • Typehints • Composer • Exceptions • Closures • Iterators CC BY-NC 4.0 Justin Yost 30

Slide 31

Slide 31 text

Takeaways • Learn and keep up with your language CC BY-NC 4.0 Justin Yost 31

Slide 32

Slide 32 text

Thanks/Questions? • twitter.com/justinyost • github.com/justinyost • justinyost.com • loadsys.com • lynda.com/justinyost CC BY-NC 4.0 Justin Yost 32