What’s New in PHP 7.0A the only reason why it’s worth upgrading
Andrea TelatinBMR Genomics srl
Overview and why to switch
About this release
• Released on December 3, 2015
• Major release after 11 years
• Two years in development
• New Zend Engine
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…
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
New Features
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,…
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";
}
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)
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
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;}
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
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
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} = 😀
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";
}
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";
}
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!
Deprecated things
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()
Deleted things
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? <?= … ?>
Other deletions
• ereg_ functions to preg_ functions
• mysql_ functions to mysqli_ functions
• mcrypt_generic_end, mcrypt_cbc……..
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