The PHP 5.4 Features You Will Actually Use About Me Lorna Jane - - PowerPoint PPT Presentation

the php 5 4 features you will actually use about me
SMART_READER_LITE
LIVE PREVIEW

The PHP 5.4 Features You Will Actually Use About Me Lorna Jane - - PowerPoint PPT Presentation

The PHP 5.4 Features You Will Actually Use About Me Lorna Jane Mitchell PHP Consultant/Developer Author of PHP Master Twitter: @lornajane Website: http://lornajane.net 2 About PHP 5.4 New features Traits Built-in


slide-1
SLIDE 1

The PHP 5.4 Features You Will Actually Use

slide-2
SLIDE 2

About Me

2

  • Lorna Jane Mitchell
  • PHP Consultant/Developer
  • Author of PHP Master
  • Twitter: @lornajane
  • Website: http://lornajane.net
slide-3
SLIDE 3

About PHP 5.4

3

  • New features
  • Traits
  • Built-in webserver
  • New array syntax
  • And more!
  • Removed some nonsense
  • This talk covers the best bits*

* an entirely subjective selection

slide-4
SLIDE 4

Start With The Easy Bit

slide-5
SLIDE 5

New Array Syntax

slide-6
SLIDE 6

Array Syntax

6

We had this:

$game[] = 'paper'; $game[] = 'scissors'; $game[] = 'stone'; print_r($game); $game[0] = 'paper'; $game[1] = 'scissors'; $game[2] = 'stone'; print_r($game);

slide-7
SLIDE 7

Array Syntax

7

Or this:

$game = array('stone', 'paper', 'scissors'); print_r($game); $game = array(0 => 'stone', 1 => 'paper', 2 => 'scissors'); print_r($game);

slide-8
SLIDE 8

Array Syntax

8

Now we can do:

$game = ['scissors', 'stone', 'paper']; print_r($game); $game = [0 => 'scissors', 1 => 'stone', 2 => 'paper']; print_r($game);

slide-9
SLIDE 9

PHP 5.4 Stealth Feature

slide-10
SLIDE 10

PHP 5.4 Is Faster

slide-11
SLIDE 11

PHP Versions Speed Comparison

11

Benchmarks made using:

  • Newly-compiled vanilla PHP binaries
  • The bench.php script in the PHP source tree
  • A rather average laptop
  • 10 runs per version, then averaged
slide-12
SLIDE 12

PHP Versions Speed Comparison

12

slide-13
SLIDE 13

PHP Versions Speed Comparison

13

slide-14
SLIDE 14

PHP Versions Speed Comparison

14

slide-15
SLIDE 15

PHP Versions Speed Comparison

15

slide-16
SLIDE 16

Traits

slide-17
SLIDE 17

Traits

17

Re-usable groups of methods, in languages with single inheritance.

  • Declare a trait, it looks like a class
  • Add the trait to your class using the use keyword
  • Methods are available!
slide-18
SLIDE 18

Simplest Trait Example

18

<?php trait Audit { public function getAuditTrail() { return "nothing changed"; } } class Princess { use Audit; // General princess class description: // soldering, tree climbing, the usual } $daisy = new Princess(); echo $daisy->getAuditTrail();

slide-19
SLIDE 19

Other Trait Trivia

19

  • They can be aliased when used
  • There are rules about resolving naming clashes
  • Traits can include properties
  • Traits can include abstract methods
  • Traits can themselves make use of other traits
slide-20
SLIDE 20

Built In Webserver

slide-21
SLIDE 21

Built In Webserver

21

Built in application server for PHP 5.4

  • Simple
  • Lightweight
  • Development use only

From the manual: "Requests are served sequentially."

http://lrnja.net/NQoXsh

slide-22
SLIDE 22

Webserver Examples

22

Start a simple server on a port number of your choice

php -S localhost:8080

http://localhost:8080

slide-23
SLIDE 23

Webserver Examples

23

Change the hostname:

php -S dev.project.local:8080

http://dev.project.local:8080

slide-24
SLIDE 24

Webserver Examples

24

Specify the docroot

php -S localhost:8080 -t /var/www/superproject

Specify which php.ini to use (default: none)

php -S localhost:8080 -c php.ini-development

slide-25
SLIDE 25

Webserver Examples

25

Use a routing file (example from http://lrnja.net/LigI4U)

<?php if (file_exists(__DIR__ . '/' . $_SERVER['REQUEST_URI'])) { return false; // serve the requested resource as-is } else { include_once 'index.php'; }

routing.php

php -S localhost:8080 routing.php

The webserver runs routing.php before entering the requested script. This example serves any existing resource, or routes to index.php

slide-26
SLIDE 26

Session Upload Progress

slide-27
SLIDE 27

Session Upload Progress

27

File upload progress, written to the session at intervals Useful for user feedback, e.g. shiny new HTML5 progress bars!

slide-28
SLIDE 28

Tracking Upload Progress

28

User starts uploading file in the usual way

<form name="upload" method="post" enctype="multipart/form-data"> <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" /> File: <input type="file" id="file1" name="file1" /> <input type="submit" value="start upload" /> </form>

slide-29
SLIDE 29

Tracking Upload Progress

29

In a separate file, we can check the relevant session variables to see how the upload is going: http://lrnja.net/Lhs7jJ

array(1) { ["upload_progress_123"]=> array(5) { ["start_time"]=> int(1340542011) ["content_length"]=> int(1529158981) ["bytes_processed"]=> int(1386660589) ["done"]=> bool(false) ["files"]=> array(1) { [0]=> array(7) { ... } } } }

slide-30
SLIDE 30

Callable Typehint

slide-31
SLIDE 31

Typehinting

31

We can typehint in PHP , on:

  • Classes
  • Interfaces
  • Arrays
slide-32
SLIDE 32

Typehinting

31

We can typehint in PHP , on:

  • Classes
  • Interfaces
  • Arrays
  • ... and now Callable

Callable denotes anything that can be called - e.g. a closure, callback or invokable object

slide-33
SLIDE 33

Callable Examples

32

$ping = function() { echo "ping!"; }; $pong = "pong"; function sparkles(Callable $func) { $func(); return "fairy dust"; } echo sparkles($ping); // ping!fairy dust echo sparkles($pong);

Catchable fatal error: Argument 1 passed to sparkles() must be callable, string given, called in /home/lorna/.../callable.php on line 16 and defined in /home/lorna/.../callable.php on line 10

slide-34
SLIDE 34

Callable Examples

33

function sparkles(Callable $func) { $func(); return "fairy dust"; } class Butterfly { public function __invoke() { echo "flutter"; } } $bee = new Butterfly(); echo sparkles($bee); // flutterfairy dust

slide-35
SLIDE 35

JsonSerializable

slide-36
SLIDE 36

Sleep and Wakeup

35

When we serialize() or unserialize() an object, PHP provides "magic methods" for us to hook into (this isn’t new):

  • __sleep() to specify which fields should be serialized
  • __wakeup() to perform any operations needed to complete an
  • bject when it is unserialized

This gives us the same feature for when we JSON something

slide-37
SLIDE 37

The JsonSerializable Interface

36

From the manual:

JsonSerializable { abstract public mixed jsonSerialize () }

Objects implementing the JsonSerializable interface can control how they are represented in JSON when they are passed to json_encode()

slide-38
SLIDE 38

JsonSerializable Example

37

class gardenObject implements JsonSerializable { public function jsonSerialize() { unset($this->herbs); return $this; } } $garden = new gardenObject(); $garden->flowers = array("clematis", "geranium", "hydrangea"); $garden->herbs = array("mint", "sage", "chives", "rosemary"); $garden->fruit = array("apple", "rhubarb"); echo json_encode($garden);

{"flowers":["clematis","geranium","hydrangea"],"fruit" :["apple","rhubarb"]}

slide-39
SLIDE 39

PHP 5.4

slide-40
SLIDE 40

Did I Mention it’s Faster?

39

slide-41
SLIDE 41

The PHP 5.4 Features I’ll Actually Use

40

  • Short array syntax
  • Traits
  • Built in webserver
  • Upload progress
  • Callable
  • JsonSerializable
slide-42
SLIDE 42

Questions?

slide-43
SLIDE 43

Resources

42

All in a single bundle for you: http://lrnja.net/KOouXv

slide-44
SLIDE 44

Thanks!

43

@lornajane

http://lornajane.net