key features php 5.3 - 5.6

Post on 30-Jul-2015

176 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

KEY FEATRURESPHP 5.3 - 5.6

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

History

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

!

PHP 5.3

Namespaces

<?phpnamespace mostofreddy\logger;Class Logger {

…}?>

<?php

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

PHP 5.3

Lambdas & Closures

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

PHP 5.4

JSON serializable

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"]}

Session handler

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")

)

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();

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

Arrays

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

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

Built-in web server

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.

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

Traits

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

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

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

PHP 5.5

Generators

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;}

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;

}

PHP with generators

Total time: 1.3618 segMemory: 256 k

PHP classic

Total time: 1.5684 segMemory: 2.5 M

PHP 5.5

OPCache

PHP 5.5

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

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)

Password Hashing API

PHP 5.5

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

Funtions

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();

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();}

PHP 5.5

var_dump(password_get_info($hash1));

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

'cost' => int(12) }}

PHP 5.6

Variadic functions via …&&

Argument unpacking via ...

PHP < 5.6

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

// Ides count 2

PHP 5.6

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

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

// Ides count 2

PHP 5.6

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

// 10

PHP 5.6

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

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

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

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

__debugInfo()

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)}

Questions?

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

Thanks!

top related