1. What’s new in PHP 7.1
And why you really shouldn’t be using PHP5 anymore…
PHP Cambridge, 12th December 2016
Simon Jones, Studio 24
2. PHP 7.1
• Released 1st Dec 2017
• PHP 5.6 end of life 31 Dec 2018
• PHP 5.4 and 5.5 no longer supported
• WordPress now recommends PHP 7
(though still works on PHP 5.2
which was released over.. Nine. Years. Ago!)
4. Why PHP 7?
• Current, stable & supported version of PHP
• Stricter, more secure, and faster!
• Twice as fast for most applications
• Reduced memory usage
6. New features
PHP 5.5
password_hash()
Generators
Zend Optimiser+ opcode cache (OPcache)
PHP 5.6
Constant expressions
Variadic functions (...$params)
PHP 7.0
Scalar type, return type declarations
Exception hierarchy
Spaceship operator <=>
PHP 7.1
Class constant visibility
Nullable types, void return type, iterable pseudo-type
Multi catch exception handling
7. • Removed from PHP..
• mysql_* functions (use PDO)
• mssql_* functions (use PDO)
• ereg_* functions (use preg_*)
• ASP & script style tags
<% %>
<script language=“php”> </script>
• foreach no longer changes the internal array pointer
What breaks / changes in PHP 7.0?
8. • Error exception thrown when invoking user-defined
functions with too few arguments
function hello($name, $age) { }
echo hello("Simon");
• DateTime constructor includes microseconds,
be careful when comparing:
new DateTime() == new DateTime();
What breaks / changes in PHP 7.1?
9. Going, going…
• PHP 4 style constructors
class MyClass {
function MyClass() {}
}
• Static calls to non-static methods
class MyClass {
public function print() {}
}
MyClass::print();
11. • Fatal errors throw exceptions. Yay!
• Custom error handlers may no longer work
Changes to error handling
http://php.net/manual/en/language.errors.php7.php
12. Type hinting
Scalar type hinting
function hello (string $name, int $age) {
echo "Hello $name, you are nearly " . $age + 1;
}
Return types
function hello (string $name, int $age) : string {
return "Hello $name, you are nearly " . $age + 1;
}
13. Strict typing
By default, PHP will coerce values of the wrong type into
the expected scalar type if possible.
function hello (string $name, int $age) {
echo "Hello $name, you are nearly " . $age + 1;
}
This is OK
hello("Simon", "42");
14. Strict typing
Unless you enable strict mode
declare(strict_types=1);
function hello (string $name, int $age) {
echo "Hello $name, you are nearly " . $age + 1;
}
This throws a Fatal error exception
hello("Simon", "42");
15. Type hinting..
PHP 5.0
Class Name
self
PHP 5.1 array
PHP 5.4 callable
PHP 7.0
bool
float
int
string
PHP 7.1
iterable (array or Traversable)
Nullable types, e.g. ?string
void return type
16. Migration
• Upgrade to PHP5.6
• Then PHP7
• Migration guides:
http://php.net/manual/en/migration56.php
http://php.net/manual/en/migration70.php
http://php.net/manual/en/migration71.php