PHP7 brings a tremendous number of new features. Tonight, we will take a look at the null coalesce operator, new execution order (uniform variable syntax), new exceptions and more.
25. function test(string $val) {
echo $val;
}
test(new class {});
// Can’t convert: Fatal error: Uncaught
TypeError: Argument 1 passed to test()
must be of the type string, object given…
26. function test(string $val) {
echo $val;
}
test(new class {
public function __toString() {
return "Hey, PHP[tek]!";
}
});
// Outputs: string(14) "Hey, PHP[tek]!"
27. function calculateShippingTo(int $postalCode)
{
// …
}
//** separate file: **//
declare(strict_types = 1);
calculateShippingTo(“66048”);
// Throws: Catchable fatal error: Argument 1 passed
to calculateShippingTo() must be of the type
integer, string given
28. FUNCTION RETURN TYPES
• Return type goes after the function declaration:
function combine($str1, $str2): string
{
// …
}
function addProduct(): Product
{
// …
}
34. SPACESHIP OPERATOR
• Works with anything,
including arrays.
• 0 if equal
• 1 if left > right
• -1 if left < right
echo 6 <=> 6;
// Result: 0
echo 7 <=> 6;
// Result: 1
echo 6 <=> 7;
// Result: -1
58. EXECUTION ORDER CHANGES
• PHP <= 5.6 had a long list of execution order rules:
• Most of the time, the parser executed from left to
right.
• Breaking changes for some frameworks and some
code.
79. UPGRADE CONSIDERATIONS
• PHP7 is production-ready.
• Few have adopted it.
• If you release, you may encounter issues that haven’t
been discussed online.
80. UPGRADE CONSIDERATIONS
• What removed functions does your code depend on?
• https://github.com/sstalle/php7cc
• Run your unit tests against your code on a box with
PHP7.
• Try to release on a new server:
• Will give you a backup if something major happens.
81. • puphpet.com: choose PHP 7 as the version to install.
• https://www.digitalocean.com/community/tutorials/
how-to-upgrade-to-php-7-on-centos-7
• https://www.digitalocean.com/community/tutorials/
how-to-upgrade-to-php-7-on-ubuntu-14-04
INSTALLING PHP7
83. TAKE-AWAYS
• Take advantage of the new features.
• Upgrade.
• Encourage your friends about PHP7.
• Together, we will get the industry moved to PHP7!