SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
THEMOSTEXCITING
FEATURESOFPHP7.1
by
Senior Software Engineer
, a Rogue Wave Company (USA)
, Verona, 12th May
Enrico Zimuel
Zend
PHPDay 2017
ABOUTME
Developer since 1996
Senior Software Engineer at
, a Company
Core team of and
and international speaker
Research Programmer at
Co-founder of (Italy)
Zend Rogue Wave
Apigility ZF
TEDx
Amsterdam University
PUG Torino
PHP7.1
7.1.0 released (01 Dec 2016)
Latest is 7.1.5 (11 May 2017)
7.1BYNUMBERS
376 bug xes
12 new features
13 new functions
36 new global constants
20 backward incompatible changes
2 deprecated features
16 changed functions
7 other changes
NEWFEATURES
NULLABLETYPES
For parameters and return values
Pre xing the type name with a ?
NULL can be passed as an argument,
or returned as a value
EXAMPLE
function hi(?string $name): ?string
{
if (null === $name) {
return null;
}
return 'Hello ' . $name;
}
echo hi(null); // returns null
echo hi('Enrico'); // returns 'Hello Enrico'
echo hi(); // Fatal error: Too few arguments to function hi(), 0 passed
OOPEXAMPLE
interface Fooable {
function foo(): ?Fooable;
}
interface StrictFooable extends Fooable {
function foo(): Fooable; // valid
}
interface Fooable {
function foo(): Fooable;
}
interface LooseFooable extends Fooable {
function foo(): ?Fooable; // invalid
}
OOPEXAMPLE(2)
interface Fooable {
function foo(Fooable $f);
}
interface LooseFoo extends Fooable {
function foo(?Fooable $f); // valid
}
interface Fooable {
function foo(?Fooable $f);
}
interface StrictFoo extends Fooable {
function foo(Fooable $f); // invalid
}
VOIDRETURNTYPE
function swap(&$left, &$right): void
{
if ($left === $right) {
return;
}
$tmp = $left;
$left = $right;
$right = $tmp;
}
$a = 1;
$b = 2;
var_dump(swap($a, $b), $a, $b); // null, int(2), int(1)
ARRAYDESTRUCTURING
$data = [
['foo', 'bar', 'baz']
];
[$a, $b] = $data[0];
var_dump($a, $b); // string(3) "foo", string(3) "bar"
[$a, , $c] = $data[0];
var_dump($a, $c); // string(3) "foo", string(3) "baz"
foreach ($data as [$a, $b, $c]) {
var_dump($a, $b, $c);
// string(3) "foo"
// string(3) "bar"
// string(3) "baz"
}
SUPPORTFORKEYS
Specify keys in list(), or its new shorthand [] syntax
$data = [
'a' => 'foo',
'b' => 'bar',
'c' => 'baz'
];
list('a' => $a, 'b' => $b, 'c' => $c) = $data;
var_dump($a, $b, $c);
// string(3) "foo"
// string(3) "bar"
// string(3) "baz"
['a' => $a, 'b' => $b, 'c' => $c] = $data;
var_dump($a, $b, $c);
// string(3) "foo"
// string(3) "bar"
// string(3) "baz"
NOMIXLIST()AND[]
// Not allowed
list([$a, $b], [$c, $d]) = [[1, 2], [3, 4]];
[list($a, $b), list($c, $d)] = [[1, 2], [3, 4]];
// Allowed
list(list($a, $b), list($c, $d)) = [[1, 2], [3, 4]];
[[$a, $b], [$c, $d]] = [[1, 2], [3, 4]];
ITERABLE
Added the iterable pseudo-type
It accepts array or Traversable
Can be used in parameter and return types
EXAMPLE
function foo(iterable $iterable): void
{
foreach ($iterable as $value) {
var_dump($value);
}
}
foo([1,2,3]);
foo(new ArrayIterator([1,2,3]));
CLASSCONSTVISIBILITY
class ConstDemo
{
const PUBLIC_CONST_A = 1; // default to public
public const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
private const PRIVATE_CONST = 4;
}
USINGREFLECTION
$obj = new ReflectionClass("ConstDemo");
foreach ($obj->getReflectionConstants () as $const) {
var_dump($const); // object(ReflectionClassConstant)
var_dump($const->getName());
var_dump($const->getValue());
var_dump($const->isPublic());
var_dump($const->isPrivate());
var_dump($const->isProtected());
}
Re ectionClassConstant is not documented (BUG )#74261
MULTIPLECATCH{}
try {
// Some code...
} catch (ExceptionType1 $e) {
// Code to handle the exception
} catch (ExceptionType2 $e) {
// Same code to handle the exception
} catch (Exception $e) {
// ...
}
try {
// Some code...
} catch (ExceptionType1 | ExceptionType2 $e) {
// Code to handle the exception
} catch (Exception $e) {
// ...
}
NEGATIVESTRINGOFFSETS
var_dump("abcdef"[-2]); // string(1) "e"
var_dump("abcdef"[-7]); // string(0) "", PHP Notice offset
// strpos
var_dump(strpos("aabbcc", "b", -3)); // int(3)
// get the last character of a string
$last = substr($foo, -1); // before PHP 7.1
$last = $foo[-1];
ASYNCSIGNALHANDLING
pcntl_async_signals() has been introduced to enable
asynchronous signal handling without using ticks
pcntl_async_signals(true); // turn on async signals
pcntl_signal(SIGHUP, function($sig) {
echo "SIGHUPn";
});
posix_kill(posix_getpid(), SIGHUP);
CLOSUREFROMCALLABLE
class Test
{
public function exposeFunction()
{
return Closure::fromCallable([ $this, 'privateFunction']);
}
private function privateFunction($param)
{
var_dump($param);
}
}
$privFunc = (new Test)->exposeFunction();
var_dump($privFunc); // object(Closure)
$privFunc('some value'); // string(10) "some value"
OPENSSLAEAD
Authenticated Encrypt with Associated Data (AEAD)
Support GCM and CCM encryption modes
GCM is 3x faster than CCM. See this benchmark
More info on Authenticated Encryption in PHP 7.1
OPENSSL_ENCRYPT()
string openssl_encrypt(
string $data,
string $method,
string $password,
[ int $options = 0 ],
[ string $iv = "" ],
[ string &$tag = NULL ],
[ string $aad = "" ],
[ int $tag_length = 16 ]
)
$tag contains the authentication hash
OPENSSL_DECRYPT()
string openssl_decrypt(
string $data,
string $method,
string $password,
[ int $options = 0 ],
[ string $iv = "" ],
[ string $tag = "" ],
[ string $aad = "" ]
)
$tag is the authentication hash
ENCRYPTEXAMPLE
$algo = 'aes-256-gcm';
$iv = random_bytes(openssl_cipher_iv_length( $algo));
$key = random_bytes(32); // 256 bit
$data = random_bytes(1024); // 1 Kb of random data
$ciphertext = openssl_encrypt(
$data,
$algo,
$key,
OPENSSL_RAW_DATA,
$iv,
$tag
);
// output is $ciphertext and $tag
$tag is the authentication hash
DECRYPTEXAMPLE
$decrypt = openssl_decrypt(
$ciphertext,
$algo,
$key,
OPENSSL_RAW_DATA,
$iv,
$tag
);
if (false === $decrypt) {
throw new Exception(sprintf(
"OpenSSL error: %s", openssl_error_string()
));
}
printf ("Decryption %sn", $data === $decrypt ? 'Ok' : 'Failed');
$tag is the authentication hash
HTTP/2SERVERPUSH
Server push has been added to CURL 7.46+
Use curl_multi_setopt() function with the new
CURLMOPT_PUSHFUNCTION constant
Added CURL_PUSH_OK and CURL_PUSH_DENY to
approve or deny the callback execution
EXAMPLE
$transfers = 1;
$callback = function($parent_ch, $pushed_ch, array $headers)
use (&$transfers) {
$transfers++;
return CURL_PUSH_OK;
};
$mh = curl_multi_init();
curl_multi_setopt($mh, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
curl_multi_setopt($mh, CURLMOPT_PUSHFUNCTION, $callback);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://localhost:8080/index.html" );
curl_setopt($ch, CURLOPT_HTTP_VERSION, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // self-signed cert
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // self-signed cert
curl_multi_add_handle($mh, $ch);
EXAMPLE(2)
$active = null;
do {
$status = curl_multi_exec($mh, $active);
do {
$info = curl_multi_info_read( $mh);
if (false !== $info && $info['msg'] == CURLMSG_DONE) {
$handle = $info['handle'];
if ($handle !== null) {
$transfers--; // decrement remaining requests
$body = curl_multi_getcontent( $info['handle']);
curl_multi_remove_handle( $mh, $handle);
curl_close( $handle);
}
}
} while ($info);
} while ($transfers);
curl_multi_close($mh);
NEWHASHFUNCTIONS
Added hash_hkdf() function to support HKDF ( )RFC 5869
$key = random_bytes(32);
$salt = random_bytes(16);
$encryptKey = hash_hkdf('sha256', $key, 32, 'encrypt', $salt);
$authKey = hash_hkdf('sha256', $key, 32, 'auth', $salt);
var_dump($encryptKey !== $authKey); // bool(true)
Added SHA3 support (224, 256, 384, and 512)
$hash = hash('sha3-224', 'This is a text');
var_dump($hash);
// string(56)"9209f5869ad03ac11549902b3c83fe8e6b7e1cd1614ab4291587db43"
TOSUMMARIZE
Nullable and void return types
Array destructuring
Iterable
Class CONST visibility
Multiple catch {}
Async signal handling
Closure from callable
OpenSSL AEAD
HTTP/2 Server Push
New hash functions
THANKS!
Rate this talk at https://joind.in/talk/935fc
Contact me: enrico.zimuel [at] roguewave.com
Follow me: @ezimuel
This work is licensed under a
.
I used to make this presentation.
Creative Commons Attribution-ShareAlike 3.0 Unported License
reveal.js

Más contenido relacionado

La actualidad más candente

Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
Refactoring to symfony components
Refactoring to symfony componentsRefactoring to symfony components
Refactoring to symfony components
Michael Peacock
 
Yy
YyYy
Yy
yygh
 
Nouveau document texte
Nouveau document texteNouveau document texte
Nouveau document texte
Sai Ef
 

La actualidad más candente (20)

Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tie
 
Advanced modulinos trial
Advanced modulinos trialAdvanced modulinos trial
Advanced modulinos trial
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find Fraudsters
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
 
ekb.py - Python VS ...
ekb.py - Python VS ...ekb.py - Python VS ...
ekb.py - Python VS ...
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
 
Parsing JSON with a single regex
Parsing JSON with a single regexParsing JSON with a single regex
Parsing JSON with a single regex
 
Meet up symfony 16 juin 2017 - Les PSR
Meet up symfony 16 juin 2017 -  Les PSRMeet up symfony 16 juin 2017 -  Les PSR
Meet up symfony 16 juin 2017 - Les PSR
 
Refactoring to symfony components
Refactoring to symfony componentsRefactoring to symfony components
Refactoring to symfony components
 
zinno
zinnozinno
zinno
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 
C99
C99C99
C99
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 Version
 
Proxy OOP Pattern in PHP
Proxy OOP Pattern in PHPProxy OOP Pattern in PHP
Proxy OOP Pattern in PHP
 
Yy
YyYy
Yy
 
Nouveau document texte
Nouveau document texteNouveau document texte
Nouveau document texte
 

Similar a The most exciting features of PHP 7.1

Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
Iftekhar Eather
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
jsmith92
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
Hari K T
 
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Masahiro Nagano
 

Similar a The most exciting features of PHP 7.1 (20)

Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
 
PHP Tips & Tricks
PHP Tips & TricksPHP Tips & Tricks
PHP Tips & Tricks
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et Pimple
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
 
Php functions
Php functionsPhp functions
Php functions
 
Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
 
PHP 5.4
PHP 5.4PHP 5.4
PHP 5.4
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
Javascript
JavascriptJavascript
Javascript
 
Txjs
TxjsTxjs
Txjs
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
 
Extbase and Beyond
Extbase and BeyondExtbase and Beyond
Extbase and Beyond
 

Más de Zend by Rogue Wave Software

Más de Zend by Rogue Wave Software (20)

Develop microservices in php
Develop microservices in phpDevelop microservices in php
Develop microservices in php
 
Speed and security for your PHP application
Speed and security for your PHP applicationSpeed and security for your PHP application
Speed and security for your PHP application
 
Building and managing applications fast for IBM i
Building and managing applications fast for IBM iBuilding and managing applications fast for IBM i
Building and managing applications fast for IBM i
 
Building web APIs in PHP with Zend Expressive
Building web APIs in PHP with Zend ExpressiveBuilding web APIs in PHP with Zend Expressive
Building web APIs in PHP with Zend Expressive
 
To PHP 7 and beyond
To PHP 7 and beyondTo PHP 7 and beyond
To PHP 7 and beyond
 
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018) Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
 
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)The Sodium crypto library of PHP 7.2 (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
 
Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)
 
Middleware web APIs in PHP 7.x
Middleware web APIs in PHP 7.xMiddleware web APIs in PHP 7.x
Middleware web APIs in PHP 7.x
 
Ongoing management of your PHP 7 application
Ongoing management of your PHP 7 applicationOngoing management of your PHP 7 application
Ongoing management of your PHP 7 application
 
Developing web APIs using middleware in PHP 7
Developing web APIs using middleware in PHP 7Developing web APIs using middleware in PHP 7
Developing web APIs using middleware in PHP 7
 
The Docker development template for PHP
The Docker development template for PHPThe Docker development template for PHP
The Docker development template for PHP
 
Unit testing for project managers
Unit testing for project managersUnit testing for project managers
Unit testing for project managers
 
The new features of PHP 7
The new features of PHP 7The new features of PHP 7
The new features of PHP 7
 
Deploying PHP apps on the cloud
Deploying PHP apps on the cloudDeploying PHP apps on the cloud
Deploying PHP apps on the cloud
 
Data is dead. Long live data!
Data is dead. Long live data! Data is dead. Long live data!
Data is dead. Long live data!
 
Optimizing performance
Optimizing performanceOptimizing performance
Optimizing performance
 
Resolving problems & high availability
Resolving problems & high availabilityResolving problems & high availability
Resolving problems & high availability
 
Developing apps faster
Developing apps fasterDeveloping apps faster
Developing apps faster
 
Keeping up with PHP
Keeping up with PHPKeeping up with PHP
Keeping up with PHP
 

Último

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Último (20)

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

The most exciting features of PHP 7.1

  • 1. THEMOSTEXCITING FEATURESOFPHP7.1 by Senior Software Engineer , a Rogue Wave Company (USA) , Verona, 12th May Enrico Zimuel Zend PHPDay 2017
  • 2. ABOUTME Developer since 1996 Senior Software Engineer at , a Company Core team of and and international speaker Research Programmer at Co-founder of (Italy) Zend Rogue Wave Apigility ZF TEDx Amsterdam University PUG Torino
  • 3. PHP7.1 7.1.0 released (01 Dec 2016) Latest is 7.1.5 (11 May 2017)
  • 4. 7.1BYNUMBERS 376 bug xes 12 new features 13 new functions 36 new global constants 20 backward incompatible changes 2 deprecated features 16 changed functions 7 other changes
  • 6. NULLABLETYPES For parameters and return values Pre xing the type name with a ? NULL can be passed as an argument, or returned as a value
  • 7. EXAMPLE function hi(?string $name): ?string { if (null === $name) { return null; } return 'Hello ' . $name; } echo hi(null); // returns null echo hi('Enrico'); // returns 'Hello Enrico' echo hi(); // Fatal error: Too few arguments to function hi(), 0 passed
  • 8. OOPEXAMPLE interface Fooable { function foo(): ?Fooable; } interface StrictFooable extends Fooable { function foo(): Fooable; // valid } interface Fooable { function foo(): Fooable; } interface LooseFooable extends Fooable { function foo(): ?Fooable; // invalid }
  • 9. OOPEXAMPLE(2) interface Fooable { function foo(Fooable $f); } interface LooseFoo extends Fooable { function foo(?Fooable $f); // valid } interface Fooable { function foo(?Fooable $f); } interface StrictFoo extends Fooable { function foo(Fooable $f); // invalid }
  • 10. VOIDRETURNTYPE function swap(&$left, &$right): void { if ($left === $right) { return; } $tmp = $left; $left = $right; $right = $tmp; } $a = 1; $b = 2; var_dump(swap($a, $b), $a, $b); // null, int(2), int(1)
  • 11. ARRAYDESTRUCTURING $data = [ ['foo', 'bar', 'baz'] ]; [$a, $b] = $data[0]; var_dump($a, $b); // string(3) "foo", string(3) "bar" [$a, , $c] = $data[0]; var_dump($a, $c); // string(3) "foo", string(3) "baz" foreach ($data as [$a, $b, $c]) { var_dump($a, $b, $c); // string(3) "foo" // string(3) "bar" // string(3) "baz" }
  • 12. SUPPORTFORKEYS Specify keys in list(), or its new shorthand [] syntax $data = [ 'a' => 'foo', 'b' => 'bar', 'c' => 'baz' ]; list('a' => $a, 'b' => $b, 'c' => $c) = $data; var_dump($a, $b, $c); // string(3) "foo" // string(3) "bar" // string(3) "baz" ['a' => $a, 'b' => $b, 'c' => $c] = $data; var_dump($a, $b, $c); // string(3) "foo" // string(3) "bar" // string(3) "baz"
  • 13. NOMIXLIST()AND[] // Not allowed list([$a, $b], [$c, $d]) = [[1, 2], [3, 4]]; [list($a, $b), list($c, $d)] = [[1, 2], [3, 4]]; // Allowed list(list($a, $b), list($c, $d)) = [[1, 2], [3, 4]]; [[$a, $b], [$c, $d]] = [[1, 2], [3, 4]];
  • 14. ITERABLE Added the iterable pseudo-type It accepts array or Traversable Can be used in parameter and return types
  • 15. EXAMPLE function foo(iterable $iterable): void { foreach ($iterable as $value) { var_dump($value); } } foo([1,2,3]); foo(new ArrayIterator([1,2,3]));
  • 16. CLASSCONSTVISIBILITY class ConstDemo { const PUBLIC_CONST_A = 1; // default to public public const PUBLIC_CONST_B = 2; protected const PROTECTED_CONST = 3; private const PRIVATE_CONST = 4; }
  • 17. USINGREFLECTION $obj = new ReflectionClass("ConstDemo"); foreach ($obj->getReflectionConstants () as $const) { var_dump($const); // object(ReflectionClassConstant) var_dump($const->getName()); var_dump($const->getValue()); var_dump($const->isPublic()); var_dump($const->isPrivate()); var_dump($const->isProtected()); } Re ectionClassConstant is not documented (BUG )#74261
  • 18. MULTIPLECATCH{} try { // Some code... } catch (ExceptionType1 $e) { // Code to handle the exception } catch (ExceptionType2 $e) { // Same code to handle the exception } catch (Exception $e) { // ... } try { // Some code... } catch (ExceptionType1 | ExceptionType2 $e) { // Code to handle the exception } catch (Exception $e) { // ... }
  • 19. NEGATIVESTRINGOFFSETS var_dump("abcdef"[-2]); // string(1) "e" var_dump("abcdef"[-7]); // string(0) "", PHP Notice offset // strpos var_dump(strpos("aabbcc", "b", -3)); // int(3) // get the last character of a string $last = substr($foo, -1); // before PHP 7.1 $last = $foo[-1];
  • 20. ASYNCSIGNALHANDLING pcntl_async_signals() has been introduced to enable asynchronous signal handling without using ticks pcntl_async_signals(true); // turn on async signals pcntl_signal(SIGHUP, function($sig) { echo "SIGHUPn"; }); posix_kill(posix_getpid(), SIGHUP);
  • 21. CLOSUREFROMCALLABLE class Test { public function exposeFunction() { return Closure::fromCallable([ $this, 'privateFunction']); } private function privateFunction($param) { var_dump($param); } } $privFunc = (new Test)->exposeFunction(); var_dump($privFunc); // object(Closure) $privFunc('some value'); // string(10) "some value"
  • 22. OPENSSLAEAD Authenticated Encrypt with Associated Data (AEAD) Support GCM and CCM encryption modes GCM is 3x faster than CCM. See this benchmark More info on Authenticated Encryption in PHP 7.1
  • 23. OPENSSL_ENCRYPT() string openssl_encrypt( string $data, string $method, string $password, [ int $options = 0 ], [ string $iv = "" ], [ string &$tag = NULL ], [ string $aad = "" ], [ int $tag_length = 16 ] ) $tag contains the authentication hash
  • 24. OPENSSL_DECRYPT() string openssl_decrypt( string $data, string $method, string $password, [ int $options = 0 ], [ string $iv = "" ], [ string $tag = "" ], [ string $aad = "" ] ) $tag is the authentication hash
  • 25. ENCRYPTEXAMPLE $algo = 'aes-256-gcm'; $iv = random_bytes(openssl_cipher_iv_length( $algo)); $key = random_bytes(32); // 256 bit $data = random_bytes(1024); // 1 Kb of random data $ciphertext = openssl_encrypt( $data, $algo, $key, OPENSSL_RAW_DATA, $iv, $tag ); // output is $ciphertext and $tag $tag is the authentication hash
  • 26. DECRYPTEXAMPLE $decrypt = openssl_decrypt( $ciphertext, $algo, $key, OPENSSL_RAW_DATA, $iv, $tag ); if (false === $decrypt) { throw new Exception(sprintf( "OpenSSL error: %s", openssl_error_string() )); } printf ("Decryption %sn", $data === $decrypt ? 'Ok' : 'Failed'); $tag is the authentication hash
  • 27. HTTP/2SERVERPUSH Server push has been added to CURL 7.46+ Use curl_multi_setopt() function with the new CURLMOPT_PUSHFUNCTION constant Added CURL_PUSH_OK and CURL_PUSH_DENY to approve or deny the callback execution
  • 28. EXAMPLE $transfers = 1; $callback = function($parent_ch, $pushed_ch, array $headers) use (&$transfers) { $transfers++; return CURL_PUSH_OK; }; $mh = curl_multi_init(); curl_multi_setopt($mh, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); curl_multi_setopt($mh, CURLMOPT_PUSHFUNCTION, $callback); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://localhost:8080/index.html" ); curl_setopt($ch, CURLOPT_HTTP_VERSION, 3); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // self-signed cert curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // self-signed cert curl_multi_add_handle($mh, $ch);
  • 29. EXAMPLE(2) $active = null; do { $status = curl_multi_exec($mh, $active); do { $info = curl_multi_info_read( $mh); if (false !== $info && $info['msg'] == CURLMSG_DONE) { $handle = $info['handle']; if ($handle !== null) { $transfers--; // decrement remaining requests $body = curl_multi_getcontent( $info['handle']); curl_multi_remove_handle( $mh, $handle); curl_close( $handle); } } } while ($info); } while ($transfers); curl_multi_close($mh);
  • 30. NEWHASHFUNCTIONS Added hash_hkdf() function to support HKDF ( )RFC 5869 $key = random_bytes(32); $salt = random_bytes(16); $encryptKey = hash_hkdf('sha256', $key, 32, 'encrypt', $salt); $authKey = hash_hkdf('sha256', $key, 32, 'auth', $salt); var_dump($encryptKey !== $authKey); // bool(true) Added SHA3 support (224, 256, 384, and 512) $hash = hash('sha3-224', 'This is a text'); var_dump($hash); // string(56)"9209f5869ad03ac11549902b3c83fe8e6b7e1cd1614ab4291587db43"
  • 31. TOSUMMARIZE Nullable and void return types Array destructuring Iterable Class CONST visibility Multiple catch {} Async signal handling Closure from callable OpenSSL AEAD HTTP/2 Server Push New hash functions
  • 32. THANKS! Rate this talk at https://joind.in/talk/935fc Contact me: enrico.zimuel [at] roguewave.com Follow me: @ezimuel This work is licensed under a . I used to make this presentation. Creative Commons Attribution-ShareAlike 3.0 Unported License reveal.js