Slide 1

Slide 1 text

Awesome New OOP Stuff in Kristopher Wilson [email protected] twitter.com/mrkrstphr github.com/mrkrstphr

Slide 2

Slide 2 text

PHP < 5.0 ■ OOP didn't really exist. Sure there were classes; that's about it.

Slide 3

Slide 3 text

PHP 5 ■ Released: 13 July 2004 ■ Really real OOP! ■ Cool new things like: Abstract Class, Interface, Public/Private/Protected, Constructors, Destructors! ■ Subsequent releases: ○ 5.1: 24 Nov 2005 ○ 5.2: 02 Nov 2006 ○ 6.0: LOL

Slide 4

Slide 4 text

PHP 5.3

Slide 5

Slide 5 text

PHP 5.3 ■ Released: 30 Jun 2009 ■ End of Life: March 2013 ■ UPGRADE!

Slide 6

Slide 6 text

Namespaces

Slide 7

Slide 7 text

PHP 5.3: Namespaces What is a namespace?

Slide 8

Slide 8 text

PHP 5.3: Namespaces What is a namespace? Named collection of "stuff" Classes! Functions! Constants! Variables! Interfaces!

Slide 9

Slide 9 text

PHP 5.3: Namespaces What is a namespace? Named collection of "stuff" Classes! Functions! Constants! Variables! Interfaces! Sounds like some messy code...

Slide 10

Slide 10 text

PHP 5.3: Namespaces A namespace is a declared, named scoping mechanism

Slide 11

Slide 11 text

PHP 5.3: Namespaces A namespace is a declared, named scoping mechanism A namespace must be the first line of code in a file. Unless you have multiple namespaces in one file...

Slide 12

Slide 12 text

PHP 5.3: Namespaces A namespace is a declared, named scoping mechanism A namespace must be the first line of code in a file. Unless you have multiple namespaces in one file...

Slide 13

Slide 13 text

PHP 5.3: Namespaces Classes are declared in a namespace and belong to that namespace.

Slide 14

Slide 14 text

PHP 5.3: Namespaces

Slide 15

Slide 15 text

PHP 5.3: Namespaces Everything is in a namespace! ■ Since PHP 5.3, every class, function, variable, constant, etc is located within a namespace, even if you don't use them. ■ If it isn't explicitly in a namespace, it is in the global namespace (or global scope). ■ The global namespace in PHP is called: \

Slide 16

Slide 16 text

PHP 5.3: Namespaces When working inside of a namespace, all internal PHP classes are located outside of your namespace. To access them, you must precede them by their namespace name (\). '); } }

Slide 17

Slide 17 text

PHP 5.3: Namespaces setOther(new OtherClass()); The use keyword lets you import a resources from a namespace into your current scope. This helps keep classnames short. A good IDE will generate the use statements for you.

Slide 18

Slide 18 text

PHP 5.3: Namespaces '); } } Even classes in the global scope can be imported.

Slide 19

Slide 19 text

PHP 5.3: Namespaces

Slide 20

Slide 20 text

PHP 5.3: Namespaces

Slide 21

Slide 21 text

PHP 5.3: Namespaces

Slide 22

Slide 22 text

PHP 5.3: Namespaces In fact... The PSR-0 PHP standard defines the way in which you should setup your namespaces. ■ A namespace must begin with a Vendor Name ■ The second part should be the name of the package ■ Further hierarchy is up to the developer, but usually is grouped by component. Example: namespace Doctrine\ORM\Mapping;

Slide 23

Slide 23 text

PHP 5.3: Namespaces PSR-0 Directory Mapping Further, your namespace hierarchy should map 1 for 1 with your directory structure in your source code directory. Namespace: namespace Doctrine\ORM\Mapping; Source Code: /path/to/MyProject/src/Doctrine/ORM/Mapping /Annotation.php /Column.php

Slide 24

Slide 24 text

PHP 5.3: Namespaces PSR-0 Namespace Standard Following PSR-0 allows for autoloader interoperability. This means that, when following PSR-0, resources in your source or library can be autoloaded like any other PSR-0 conforming library. It also makes package managers like Composer work well. Learn more: https://github.com/php-fig

Slide 25

Slide 25 text

PHP 5.3: Namespaces Why is this so awesome? ■ Clean organization of your code ○ Aids in separation of concerns by giving "packages" ○ Allows use of shorter class names ■ Easily use 3rd party libraries without conflict (aliasing) ■ Provides a common format for autoloader interoperability (PSR-0)

Slide 26

Slide 26 text

Late Static Binding

Slide 27

Slide 27 text

PHP 5.3: Late Static Binding Previously in PHP, when using a static class, you couldn't access an inherited, overridden method or variable.

Slide 28

Slide 28 text

PHP 5.3: Late Static Binding

Slide 29

Slide 29 text

PHP 5.3: Late Static Binding

Slide 30

Slide 30 text

Late Static Binding!

Slide 31

Slide 31 text

PHP 5.3: Late Static Binding

Slide 32

Slide 32 text

PHP 5.3: Late Static Binding

Slide 33

Slide 33 text

PHP 5.3: Late Static Binding Now in PHP, you can totally access an inherited, overridden method or variable. ■ The self keyword refers to whatever class it is used in, regardless of inheritance. ■ The static keyword refers to whatever class it is called on, and is evaluated when called, instead of where declared. That being said, limit your use of static classes. They can make testing your code more difficult.

Slide 34

Slide 34 text

PHP 5.4

Slide 35

Slide 35 text

PHP 5.4 ■ Released: 01 March 2012

Slide 36

Slide 36 text

Traits

Slide 37

Slide 37 text

PHP 5.4: Traits ■ PHP has a single inheritance model. You can only inherit from one class. ■ Sure, you can implement oodles of interfaces, but you still have to rewrite the implementation several times. ■ This is not DRY. ■ PHP 5.4 solved this problem by introducing traits.

Slide 38

Slide 38 text

PHP 5.4: Traits What is a trait?

Slide 39

Slide 39 text

PHP 5.4: Traits What is a trait? A trait is a reusable, defined set of methods and variables that any number of classes can "import" into itself. It's like inheritance, but not quite.

Slide 40

Slide 40 text

PHP 5.4: Traits Defined like classes firstName = $firstName; } public function getFirstName() { return $this->firstName; } public function setLastName($lastName) { $this->lastName = $lastName; } public function getLastName() { return $this->lastName; } }

Slide 41

Slide 41 text

PHP 5.4: Traits "Imported" into Classes

Slide 42

Slide 42 text

PHP 5.4: Traits Use Multiple Traits!

Slide 43

Slide 43 text

PHP 5.4: Traits Now things have stuff! setFirstName('Carl') ->setLastName('Winslow');

Slide 44

Slide 44 text

PHP 5.4: Traits Benefits ■ Reusable code ■ Identical syntax (methods, behavior) for things that have the same attributes and behaviors ■ Multiple inheritance. Things can be more than one thing!

Slide 45

Slide 45 text

Short Array Syntax (not OOP)

Slide 46

Slide 46 text

PHP 5.4: Short Array Syntax Long gone are the days of using the array() language construct! $users = array('Bob', 'Sue', 'Frank', 'Thomas'); $users = ['Bob', 'Sue', 'Frank', 'Thomas'];

Slide 47

Slide 47 text

PHP 5.4: Short Array Syntax Long gone are the days of using the array() language construct! $users = array('Bob', 'Sue', 'Frank', 'Thomas'); $users = ['Bob', 'Sue', 'Frank', 'Thomas']; This. Is. So. Awesome.

Slide 48

Slide 48 text

Function Array Return Dereferencing (can be helpful with OOP)

Slide 49

Slide 49 text

PHP 5.4: Function Array Return Dereferencing Code using functions (or methods!) that return arrays can now use the result immediately, without having to store in a variable. 'Cabrera', 'firstName' => 'Miguel' } echo getUser()['lastName'] . "\n"; Variables are so 2012.

Slide 50

Slide 50 text

Short Echo Tags (not OOP)

Slide 51

Slide 51 text

PHP 5.4: Short Echo Tags ■ Always on. Always awesome
    users as $user): ?>
  • getLastName() ?>, getFirstName() ?>
    getHobbiesInterestsAndThoughtsOnLife() ?>

Slide 52

Slide 52 text

PHP 5.5

Slide 53

Slide 53 text

PHP 5.5 ■ Released: 18 Jul 2013 ■ Generators: Return early from functions. ■ try { } catch {} finally {} (finally) ■ Foreach now supports list() foreach ($array as list($a, $b)) {} ■ Password Hashing API