php 7.0 new features (and new interpreter)

27
What’s New in PHP 7.0 A the only reason why it’s worth upgrading Andrea Telatin BMR Genomics srl

Upload: andrea-telatin

Post on 13-Apr-2017

236 views

Category:

Engineering


5 download

TRANSCRIPT

Page 1: PHP 7.0 new features (and new interpreter)

What’s New in PHP 7.0A the only reason why it’s worth upgrading

Andrea TelatinBMR Genomics srl

Page 2: PHP 7.0 new features (and new interpreter)

Overview and why to switch

Page 3: PHP 7.0 new features (and new interpreter)

About this release

• Released on December 3, 2015

• Major release after 11 years

• Two years in development

• New Zend Engine

Page 4: PHP 7.0 new features (and new interpreter)

What happened to 6.0?• Had the goal to support Unicode everywhere

• Too ambitious?Goal dropped, yet after 5 years of conferences, blogs, …

• Features of “6.0” entered in the 5.x

• namespaces, closures, short array syntax…

Page 5: PHP 7.0 new features (and new interpreter)

Performance PHP 7.0 has significantly better performance

• Probably the only reason to upgrade.

• Optimized parsing, and lower memory ~2X faster

• Compares with HHVM (FaceBook)

• http://www.zend.com/en/resources/php7_infographic

Page 6: PHP 7.0 new features (and new interpreter)
Page 7: PHP 7.0 new features (and new interpreter)
Page 8: PHP 7.0 new features (and new interpreter)
Page 9: PHP 7.0 new features (and new interpreter)

New Features

Page 10: PHP 7.0 new features (and new interpreter)

Type hinting

• Specify the type of function arguments

• Can be done in three ways: none, coercitive, strict

• Types available: int, string, bool, array, float

• I think will be super cool for IDEs,…

Page 11: PHP 7.0 new features (and new interpreter)

Type hinting• Specify the type of function arguments

• Can be done in three ways: none, coercitive or strict

function SumNumbers($first, $second) {echo $first . " (". gettype($first) . " + \n";echo $second ." (". gettype($second). " = \n";$result = $first + $second;echo $result ." (". gettype($result).")\n";

}

Page 12: PHP 7.0 new features (and new interpreter)

Type hintingSumNumbers(1, 5); SumNumbers(1.0, 5.0); // What happens?SumNumbers(“1”, “5”); // What happens?

• Coercitive declaration:function SumNumbers(int $first,int $second) { …// PHP immediately convert input into integers

• Strict declaration:declare(strict_type=1);function SumNumbers(int $first,int $second) { …// error if we don’t pass integer (type error)

Page 13: PHP 7.0 new features (and new interpreter)

Return type declaration

• Specify the type of what we return from functions

• Again can be done in three ways: none, coercitive, strict

• Again types available: int, string, bool, array, float

Page 14: PHP 7.0 new features (and new interpreter)

Return type declaration• Coercive (convert the result if needed) function SumNumbers($first, $second): int {

$result = $first + $second; return $result;}

• Strict (return error if the type doesn’t match)declare(strict_types = 1);function SumNumbers($first, $second): int {

return $first + $second;}

Page 15: PHP 7.0 new features (and new interpreter)

New function intdiv()

• Yes, its for integer division

echo intdiv(4, 3);// Always return an int. // More precision avoiding float! // compare with floor($bignum/2);

• Completes the modulus operator

Page 16: PHP 7.0 new features (and new interpreter)

Uniform variable syntax• When we used variable variable-names:

$var_name = 'FirstName';$FirstName = 'Andrea';$Person = ('Name' => 'Andrea', 'Surname' => 'Telatin');

echo $$var_name; // prints Andreaecho $$Person['Name']; // Illegal in PHP5, valid in 7

• Can be used for nested variables

Page 17: PHP 7.0 new features (and new interpreter)

Unicode

• A codepoint represents a character/symbol/emoji

• Encoded as hex number, eg 2603 is

• In PHP is like print "Hello from \u{2603}"

• \u{1F600} = 😀

Page 18: PHP 7.0 new features (and new interpreter)

Catch exceptions: PHP 5function getGeneCoord($ensembleID) {

if (isnull($ensembleID) {throw new Exception('No ID provided');

}return $ensembleID->cordinates;

}

try {print getGeneCoord($id)."\n";

} catch (Exception $e) {echo "Raised exception: $e\n";

}

Page 19: PHP 7.0 new features (and new interpreter)

Catch exceptions: PHP 7function getGeneCoord($ensembleID) {

if (isnull($ensembleID) {throw new Exception('No ID provided');

}return $ensembleID->cordinates;

}

try {print getGeneCoord($id)."\n";

} catch (Error $e) {echo "Raised exception: $e\n";

}

Page 20: PHP 7.0 new features (and new interpreter)

Other new features• Anonymous classes in OOP: quick, one-time use

classes (omit class name)$instance = new class { … }

• Null coalescent operator: ?? Return the first not-null value in a series $username = $db[name] ?? $default;

• Spaceship operator: <=>// -1 if <, 0 if ==, 1 if >… Useful in switch

and more!

Page 21: PHP 7.0 new features (and new interpreter)

Deprecated things

Page 22: PHP 7.0 new features (and new interpreter)

PHP 4 constructors

• Class with a method with the same name as the class. Called during creation.

• Since PHP 5 the common practice is calling the method _constructor()

Page 23: PHP 7.0 new features (and new interpreter)

Deleted things

Page 24: PHP 7.0 new features (and new interpreter)

Alternative PHP marks• The standard tags are

<?php code here ?>

• Deleted alternatives:<% ASP style %><%= ASP style with echo %> <script language="php"> html style </script>

• Short open tags still OK but will they for long? <?= … ?>

Page 25: PHP 7.0 new features (and new interpreter)

Other deletions

• ereg_ functions to preg_ functions

• mysql_ functions to mysqli_ functions

• mcrypt_generic_end, mcrypt_cbc……..

Page 26: PHP 7.0 new features (and new interpreter)

Timezone warnings

• PHP5 fired a warning if you didn’t set date.timezone in PHP.ini file (default was UTC)

• Default is still UTC, but warning removed

Page 27: PHP 7.0 new features (and new interpreter)