php 7

23
PHP 7 What’s new in PHP 7 By: Joshua Copeland

Upload: joshua-copeland

Post on 09-Aug-2015

78 views

Category:

Software


0 download

TRANSCRIPT

Page 1: PHP 7

PHP 7What’s new in PHP 7By: Joshua Copeland

Page 2: PHP 7

Joshua Copeland PHP lover for 7+ years

Lead Developer at The Selling Source (we need a DBA)

Father, husband, and overall awesome guy

Software Developer for over 10 years

First human to be a computer

My Twitter Handle : @PsyCodeDotOrg

Page 3: PHP 7

Proposed Milestones

1. Line up any remaining RFCs that target PHP 7.0.

2. Finalize implementation & testing of new features.Mar 16 - Jun 15 (3 months)

3. Release Candidate (RC) cycles Jun 16 - Oct 15 (3 months)

4. GA/Release Mid October 2015

https://wiki.php.net/rfc/php7timeline

Page 4: PHP 7

Inconsistency Fixes Abstract Syntax Tree (AST)

Decoupled the parser from the compiler.

list() currently assigns variables right-to-left, the AST implementation will assign them left-to-right instead.

Auto-vivification order for by-reference assignments.

10-15% faster compile times, but requires more memory.

Directly calling __clone is allowed

https://wiki.php.net/rfc/abstract_syntax_tree

Page 5: PHP 7

Uniform Variable Syntax

Introduction of an internally consistent and complete variable syntax. To achieve this goal the semantics of some rarely used variable-variable constructions need to be changed.

Call closures assigned to properties using ($object->closureProperty)()

Chain static calls. Ex: $foo::$bar::$bat

Semantics when using variable-variables/properties

https://wiki.php.net/rfc/uniform_variable_syntax

Page 6: PHP 7

Biggest reason to update

Page 7: PHP 7

Biggest reason to update

Performance!

PHP 7 is on par with HHVM

Memory Savings

Page 8: PHP 7

Backwards Incompatible Changes

Call to member function on a non-object now catchable fatal error. https://wiki.php.net/rfc/catchable-call-to-member-of-non-object

ASP and script tags have been removed Ex. <% <%= <script

language=“php”></script>

Removal of all deprecated functionality. https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7

Removal of the POSIX compatible regular expressions extension, ext/ereg (deprecated in 5.3) and the old ext/mysql extension (deprecated in 5.5).

Switch statements throw and error when more than one default case found.

Page 9: PHP 7

New Features Scalar type hints (omg yes)

https://wiki.php.net/rfc/scalar_type_hints_v5

Weak comparison mode will cast to the type you wish.

Strict comparison will throw a catchable fatal. Turn on with declare(strict_types=1);

function sendHttpStatus(int $statusCode, string $message) { header('HTTP/1.0 ' .$statusCode. ' ' .$message);}

sendHttpStatus(404, "File Not Found"); // integer and string passedsendHttpStatus("403", "OK"); // string "403" coerced to int(403)

Page 10: PHP 7

New Features Return Type Hints

https://wiki.php.net/rfc/return_types

String, int, float, bool

Same rules apply with weak and strict modes as parameter type hints.

Page 11: PHP 7

New Features Combined Comparison Operator

https://wiki.php.net/rfc/combined-comparison-operator

AKA Starship Operator <=>

Great for sorting, returns -1 if value to the right, 0 if same, etc.

// Pre Spacefaring PHP 7function order_func($a, $b) { return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);}

// Post PHP 7function order_func($a, $b) { return $a <=> $b;}

Page 12: PHP 7

New Features Unicode Codepoint Escape Syntax

The addition of a new escape character, \u, allows us to specify Unicode character code points (in hexidecimal) unambiguously inside PHP strings:

The syntax used is \u{CODEPOINT}, for example the green heart, , can be expressed as the PHP string: "\💚

u{1F49A}".

Page 13: PHP 7

New Features Null Coalesce Operator

Another new operator, the Null Coalesce Operator, ??

It will return the left operand if it is not NULL, otherwise it will return the right.

The important thing is that it will not raise a notice if the left operand is a non-existent variable. This is like isset() and unlike the ?: short ternary operator.

You can also chain the operators to return the first non-null of a given set:

$config = $config ?? $this->config ?? static::$defaultConfig;

Page 14: PHP 7

New Features Bind Closure on Call

With PHP 5.4 we saw the addition of Closure->bindTo() and Closure::bind() which allows you change the binding of $this and the calling scope, together, or separately, creating a duplicate closure.

PHP 7 now adds an easy way to do this at call time, binding both $this and the calling scope to the same object with the addition of Closure->call(). This method takes the object as it’s first argument, followed by any arguments to pass into the closure, like so:

class HelloWorld { private $greeting = "Hello"; } $closure = function($whom) { echo $this->greeting . ' ' . $whom; }

$obj = new HelloWorld(); $closure->call($obj, 'World'); // Hello World

Page 15: PHP 7

New Features Group Use Declarations

// Original

use Framework\Component\SubComponent\ClassA; use Framework\Component\SubComponent\ClassB as ClassC; use Framework\Component\OtherComponent\ClassD;

// With Group Use

use Framework\Component\{ SubComponent\ClassA, SubComponent\ClassB as ClassC, OtherComponent\ClassD };

Page 16: PHP 7

New Features Group Use Declarations

It can also be used with constant and function imports with use function, and use const, as well as supporting mixed imports:

use Framework\Component\{ SubComponent\ClassA, function OtherComponent\someFunction, const OtherComponent\SOME_CONSTANT };

Page 17: PHP 7

New Features Generator Return Expressions

There are two new features added to generators. The first is Generator Return Expressions, which allows you to now return a value upon (successful) completion of a generator.

Prior to PHP 7, if you tried to return anything, this would result in an error. However, now you can call $generator->getReturn() to retrieve the return value.

If the generator has not yet returned, or has thrown an uncaught exception, calling $generator->getReturn() will throw an exception. If the generator has completed but there was no return, null is returned.

https://wiki.php.net/rfc/generator-return-expressions

Page 18: PHP 7

New Features Generator Return Expressions

function gen() { yield "Hello"; yield " "; yield "World!"; return "Goodbye Moon!”;} $gen = gen();

foreach ($gen as $value) { echo $value; } // Outputs "Hello" on iteration 1, " " on iterator 2, and "World!" on iteration 3 echo $gen->getReturn(); // Goodbye Moon!

https://wiki.php.net/rfc/generator-return-expressions

Page 19: PHP 7

New Features Generator Delegation

The second feature is much more exciting: Generator Delegation. This allows you to return another iterable structure that can itself be traversed — whether that is an array, an iterator, or another generator.

It is important to understand that iteration of sub-structures is done by the outer-most original loop as if it were a single flat structure, rather than a recursive one.

This is also true when sending data or exceptions into a generator. They are passed directly onto the sub-structure as if it were being controlled directly by the call.

This is done using the yield from <expression> syntax

Page 20: PHP 7

New Features Generator Delegation

function hello() { yield "Hello"; yield " "; yield "World!"; yield from goodbye(); }

function goodbye() { yield "Goodbye"; yield " "; yield "Moon!"; } $gen = hello(); foreach ($gen as $value) { echo $value; }

On each iteration, this will output: "Hello" " " "World!” "Goodbye" " " "Moon!"

Page 21: PHP 7

New Features Engine Exceptions

Handling of fatal and catchable fatal errors has traditionally been impossible, or at least difficult, in PHP. But with the addition of Engine Exceptions, many of these errors will now throw exceptions instead. Now, when a fatal or catchable fatal error occurs, it will throw an exception, allowing you to handle it gracefully. If you do not handle it at all, it will result in a traditional fatal error as an uncaught exception. These exceptions are \EngineException objects, and unlike all userland exceptions, do not extend the base \Exception class. This is to ensure that existing code that catches the \Exception class does not start catching fatal errors moving forward. It thus maintains backwards compatibility. In the future, if you wish to catch both traditional exceptions and engine exceptions, you will need to catch their new shared parent class, \BaseException. Additionally, parse errors in eval()’ed code will throw a \ParseException, while type mismatches will throw a \TypeException.

Page 22: PHP 7

New Features Engine Exceptions

try { nonExistentFunction(); } catch (\EngineException $e) { var_dump($e); }

object(EngineException)#1 (7) { ["message":protected]=> string(32) "Call to undefined function nonExistantFunction()" ["string":"BaseException":private]=> string(0) "" ["code":protected]=> int(1) ["file":protected]=> string(17) "engine-exceptions.php" ["line":protected]=> int(1) ["trace":"BaseException":private]=> array(0) { } ["previous":"BaseException":private]=> NULL }