key features php 5.3 - 5.6

51
KEY FEATRURES PHP 5.3 - 5.6

Upload: federico-damian-lozada-mosto

Post on 30-Jul-2015

176 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Key features PHP 5.3 - 5.6

KEY FEATRURESPHP 5.3 - 5.6

Page 2: Key features PHP 5.3 - 5.6

FEDERICO LOZADA MOSTOTwitter: @mostofreddyWeb: mostofreddy.com.arFacebook: /mostofreddyLinkedin: ar.linkedin.com/in/federicolozadamostoGithub: /mostofreddy

Page 3: Key features PHP 5.3 - 5.6

History

Page 4: Key features PHP 5.3 - 5.6

5.6.0 – 28/08/20145.5.0 – 20/06/20135.4.0 – 01/03/20125.3.0 – 30/07/20095.2.0 – 02/11/20065.1.0 – 24/11/20055.0.0 – 13/07/2004

Fuentes:✓ http://php.net/eol.php✓ http://php.net/supported-versions.php

!

Page 5: Key features PHP 5.3 - 5.6

PHP 5.3

Page 6: Key features PHP 5.3 - 5.6

Namespaces

Page 7: Key features PHP 5.3 - 5.6

<?phpnamespace mostofreddy\logger;Class Logger {

…}?>

<?php

$log = new \mostofreddy\logger\Logger();$log->info(“example of namespaces”);//return example of namespaces

PHP 5.3

Page 8: Key features PHP 5.3 - 5.6

Lambdas & Closures

Page 9: Key features PHP 5.3 - 5.6

PHP 5.3$sayHello = function() { return "Hello world";};echo $sayHello();//return Hello world

$text = "Hello %s";$sayHello = function($name) use (&$text) { return sprintf($text, $name);};echo $sayHello("phpbsas");//return Hello phpbsas

Page 10: Key features PHP 5.3 - 5.6

PHP 5.4

Page 11: Key features PHP 5.3 - 5.6

JSON serializable

Page 12: Key features PHP 5.3 - 5.6

PHP 5.4class Freddy implements JsonSerializable{

public $data = [];public function __construct() {

$this->data = array( 'Federico', 'Lozada', 'Mosto' );

}public function jsonSerialize() {return $this->data;}

}echo json_encode(new Freddy());//return ["Federico","Lozada","Mosto"]//PHP < 5.4//{"data":["Federico","Lozada","Mosto"]}

Page 13: Key features PHP 5.3 - 5.6

Session handler

Page 14: Key features PHP 5.3 - 5.6

PHP < 5.4

$obj = new MySessionHandler;

session_set_save_handler(array($obj, "open"),array($obj, "close"),array($obj, "read"),array($obj, "write"),array($obj, "destroy"),array($obj, "gc")

)

Page 15: Key features PHP 5.3 - 5.6

PHP 5.4class MySessionHandler implements SessionHandlerInterface{

public function open($savePath, $sessionName) {}public function close() {}public function read($id) {}public function write($id, $data) {}public function destroy($id) {}public function gc($maxlifetime) {}

}

$handler = new MySessionHandler();session_set_save_handler($handler, true);session_start();

Page 16: Key features PHP 5.3 - 5.6

PHP 5.4function status() { $status = session_status(); if($status == PHP_SESSION_DISABLED) { echo "Session is Disabled"; } else if($status == PHP_SESSION_NONE ) { echo "Session Enabled but No Session values Created"; } else { echo "Session Enabled and Session values Created"; }}

status();//return Session Enabled but No Session values Createdsession_start();status();//return Session Enabled and Session values Created

Page 17: Key features PHP 5.3 - 5.6

Arrays

Page 18: Key features PHP 5.3 - 5.6

PHP 5.4

$array = [0, 1, 2, 3, 4];var_dump($array);

//returnarray(6) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4)}

Short syntax

Page 19: Key features PHP 5.3 - 5.6

PHP 5.4Array Deferencing

$txt = "Hello World";echo explode(" ", $txt)[0];//return Hello World

function getName() {return [

'user' => array( 'name'=>'Federico' )

];}echo getName()['user']['name']; //return Federico

Page 20: Key features PHP 5.3 - 5.6

Built-in web server

Page 21: Key features PHP 5.3 - 5.6

PHP 5.4

~/www$ php -S localhost:8080

PHP 5.4.0 Development Server started at Mon Apr 2 11:37:48 2012Listening on localhost:8080Document root is /var/wwwPress Ctrl-C to quit.

Page 22: Key features PHP 5.3 - 5.6

PHP 5.4~/www$ vim server.sh

#! /bin/bashDOCROOT="/var/www"HOST=0.0.0.0PORT=80ROUTER="/var/www/router.php"PHP=$(which php)if [ $? != 0 ] ; then

echo "Unable to find PHP"exit 1

fi$PHP -S $HOST:$PORT -t $DOCROOT $ROUTER

Page 23: Key features PHP 5.3 - 5.6

Traits

Page 24: Key features PHP 5.3 - 5.6

PHP 5.4trait File {

public function put($m) {error_log($m, 3, '/tmp/log');}}trait Log {

use File;public function addLog($m) {$this->put('LOG: '.$m);}

}class Test {

use Log;public function foo() { $this->addLog('test');}

}$obj = new Test;$obj->foo();//return LOG: test

Page 25: Key features PHP 5.3 - 5.6

PHP 5.4

trait Game {public function play() {return "Play Game";}

}trait Music {

public function play() {return "Play Music";}}class Player {

use Game, Music;}$o = new Player;echo $o->play();

Solving confict

PHP does not solve conflicts automatically

PHP Fatal error: Trait method play has not been applied,because there are collisions with other trait methodson Player in /var/www/test/test_traits.php on line 10

Page 26: Key features PHP 5.3 - 5.6

PHP 5.4trait Game {

public function play() {return "Play Game";}}trait Music {

public function play() {return "Play Music";}}class Player {

use Game, Music { Game::play as gamePlay; Music::play insteadof Game;

}}$o = new Player;echo $o->play(); //return Play Musicecho $o->gamePlay(); //return Play Game

Page 27: Key features PHP 5.3 - 5.6

PHP 5.5

Page 28: Key features PHP 5.3 - 5.6

Generators

Page 29: Key features PHP 5.3 - 5.6

PHP < 5.5function getLines($filename) {

if (!$handler = fopen($filename, 'r')) { return;

}$lines = [];while (false != $line = fgets($handler)) {

$lines[] = $line;}fclose($handler);return $lines;

}$lines = getLines('file.txt');foreach ($lines as $line) {

echo $line;}

Page 30: Key features PHP 5.3 - 5.6

PHP 5.5function getLines($filename) {

if (!$handler = fopen($filename, 'r')) { return;

}while (false != $line = fgets($handler)) {

yield $line;}fclose($handler);

}

foreach (getLines('file.txt') as $line) {echo $line;

}

Page 31: Key features PHP 5.3 - 5.6

PHP with generators

Total time: 1.3618 segMemory: 256 k

PHP classic

Total time: 1.5684 segMemory: 2.5 M

PHP 5.5

Page 32: Key features PHP 5.3 - 5.6

OPCache

Page 33: Key features PHP 5.3 - 5.6

PHP 5.5

Page 34: Key features PHP 5.3 - 5.6

PHP 5.5

opcache.enable=1opcache.enable_cli=1opcache.memory_consumption=128opcache.interned_strings_buffer=8opcache.max_accelerated_files=4000opcache.revalidate_freq=60opcache.fast_shutdown=1

Basic configuration

Page 35: Key features PHP 5.3 - 5.6

PHP 5.5Advanced configuration for PHPUnit / Symfony / Doctrine / Zend / etc

opcache.save_comments=1opcache.enable_file_override=1

Functions

opcache_reset()opcache_invalidate($filename, true)

Page 36: Key features PHP 5.3 - 5.6

Password Hashing API

Page 37: Key features PHP 5.3 - 5.6

PHP 5.5

password_get_info()password_hash()password_needs_rehash()password_verify()

Funtions

Page 38: Key features PHP 5.3 - 5.6

PHP 5.5

$hash = password_hash($password, PASSWORD_DEFAULT);//or$hash = password_hash( $password, PASSWORD_DEFAULT, //default bcrypt ['cost'=>12, 'salt'=> 'asdadlashdoahdlkuagssa']);

$user->setPass($hash);$user->save();

Page 39: Key features PHP 5.3 - 5.6

PHP 5.5$hash = $user->getPass();

if (!password_verify($password, $hash) { throw new \Exception('Invalid password');}

if (password_needs_rehash($hash, PASSWORD_DEFAULT, ['cost' => 18])) { $hash = password_hash( $password, PASSWORD_DEFAULT, ['cost' => 18] ); $user->setPass($hash); $user->save();}

Page 40: Key features PHP 5.3 - 5.6

PHP 5.5

var_dump(password_get_info($hash1));

// printsarray(3) { 'algo' => int(1) 'algoName' => string(6) "bcrypt" 'options' => array(1) {

'cost' => int(12) }}

Page 41: Key features PHP 5.3 - 5.6

PHP 5.6

Page 42: Key features PHP 5.3 - 5.6

Variadic functions via …&&

Argument unpacking via ...

Page 43: Key features PHP 5.3 - 5.6

PHP < 5.6

function ides() { $cant = func_num_args(); echo "Ides count: ".$cant;}ides('Eclipse', 'Netbeas');

// Ides count 2

Page 44: Key features PHP 5.3 - 5.6

PHP 5.6

function ides($ide, ...$ides) { echo "Ides count: ".count($ides);}ides (

'Eclipse', 'Netbeas', 'Sublime');

// Ides count 2

Page 45: Key features PHP 5.3 - 5.6

PHP 5.6

function sum(...$numbers) { $acc = 0; foreach ($numbers as $n) { $acc += $n; } return $acc;}echo sum(1, 2, 3, 4);

// 10

Page 46: Key features PHP 5.3 - 5.6

PHP 5.6

function add($a, $b) { return $a + $b;}

echo add(...[1, 2]); // 3

$a = [1, 2];echo add(...$a); // 3

Page 47: Key features PHP 5.3 - 5.6

PHP 5.6function showNames($welcome, Person ...$people) { echo $welcome.PHP_EOL; foreach ($people as $person) { echo $person->name.PHP_EOL; }}$a = new Person('Federico');$b = new Person('Damian');showNames('Welcome: ', $a, $b);

//Welcome://Federico//Damian

Page 48: Key features PHP 5.3 - 5.6

__debugInfo()

Page 49: Key features PHP 5.3 - 5.6

PHP 5.6class C { private $prop; public function __construct($val) { $this->prop = $val; } public function __debugInfo() { return [ 'propSquared' => $this->prop ** 2 ]; }}var_dump(new C(42));// object(C)#1 (1) { ["propSquared"]=> int(1764)}

Page 50: Key features PHP 5.3 - 5.6

Questions?

Page 51: Key features PHP 5.3 - 5.6

FEDERICO LOZADA MOSTOTW: @mostofreddyWeb: mostofreddy.com.arFB: mostofreddyIn: ar.linkedin.com/in/federicolozadamostoGit: mostofreddy

Thanks!