24
PHP 5.4 PHP 5.4 Vo Xuan Tien

Overview changes in PHP 5.4

Embed Size (px)

Citation preview

Page 1: Overview changes in PHP 5.4

PHP 5.4PHP 5.4

Vo Xuan Tien

Page 2: Overview changes in PHP 5.4

• Array Dereferencing• Short Open Tag• Short Array Syntax• Binary number format• Class member access on

instantiation

The little things

Page 3: Overview changes in PHP 5.4

Array Dereferencing (PHP < 5.4)

• $variables = getVariables();• $second_variable = $variables[1];

Page 4: Overview changes in PHP 5.4

Array Dereferencing (PHP >= 5.4)

• $second_variable = getVariables()[1];

• explode('.', 'menu.main_test')[0];• use with ArrayAccess interface (see

demo)

Page 5: Overview changes in PHP 5.4

Short Open Echo Tag

• <?= $hello_world ?>• <?= "May be not work on shared

host" ?>

Page 6: Overview changes in PHP 5.4

Short Array Syntax

• $a = [1, 2, 3, 4,];• $a = ['one' => 1, 'two' => 2, 'three'

=> 3, 'four' => 4,];• $a = [["nested 1", "nested 2"],

"xyz"];

Page 7: Overview changes in PHP 5.4

Binary number format

• $a = 1234; // decimal number• $a = -123; // a negative number• $a = 0123; // octal number

(equivalent to 83 decimal)• $a = 0x1A; // hexadecimal number

(equivalent to 26 decimal)• $a = 0b11111111; // binary number

(equivalent to 255 decimal)

Page 8: Overview changes in PHP 5.4

PHP < 5.4

• {• $temp = new Class();• do_something($temp);• // forgot to unset($temp);• do_other_thing();• ...• }

Page 9: Overview changes in PHP 5.4

Class member access on instantiation

• do_something(new Foo);• do_something((new Foo)->bar());

Page 10: Overview changes in PHP 5.4

• JSON Serialization Helper• Native Session Handler Interface• Objects as Functions• Callable Type-Hint• $this in Anonymous Functions• Initialized High Precision Timer

The little things

Page 11: Overview changes in PHP 5.4

JSON Serialization Helper

• class A implements sonSerializable• public function jsonSerialize()• echo json_encode (new A());

Page 12: Overview changes in PHP 5.4

Native Session Handler Interface (PHP < 5.4)• session_set_save_handler($open,

$close, $read, $write, $destroy, $gc);

Page 13: Overview changes in PHP 5.4

Native Session Handler Interface (PHP >= 5.4)• class A implements

SessionHandlerInterface• session_set_save_handler(new A);

Page 14: Overview changes in PHP 5.4

Objects as Functions __invoke

• function __invoke()• $instance = new A();• echo $instance();

Page 15: Overview changes in PHP 5.4

Callable Type-Hint

• function call_me(callable $function);• anonymous function• non-anonymous function• static method• instance method• callable object (see previous slide)

Page 16: Overview changes in PHP 5.4

$this in Anonymous Functions (Closures)

• function () { $this->test(); };• more fun in demo

Page 17: Overview changes in PHP 5.4

Initialized High Precision Timer (PHP < 5.4)

• $start = microtime(1);• /* your code here */• echo "took: ", (microtime(1) -

$start);

Page 18: Overview changes in PHP 5.4

Initialized High Precision Timer (PHP >= 5.4)

• /* your code here */• echo "took: ", (microtime(1) -

$_SERVER['REQUEST_TIME_FLOAT']);

• This fairly useless

Page 19: Overview changes in PHP 5.4

• Traits (Horizontal reuse, Multiple inheritance)

• "use" operator• Built-in CLI Web-Server• Performance Improvements

The big stuffs

Page 20: Overview changes in PHP 5.4

Trait

• Demo

Page 21: Overview changes in PHP 5.4

"use" operator

• "use" for namespace• "user" for closure• "use" for trait

Page 22: Overview changes in PHP 5.4

Built-in CLI Web-Server

• php -S localhost:8080• php -S localhost:8080 -t

/var/www/web• php -S localhost:8080 router.php

Page 23: Overview changes in PHP 5.4

Performance Improvements

• Real-life 5-20% faster• Benchmarks 15-20% faster• Memory 25% reduced

Page 24: Overview changes in PHP 5.4

Thank You

Tien Vo XuanReferences http://ilia.ws/files/confoo_php54.pdf

@tienvoxuan

tien.voxuan