A description of the most important features in the upcoming major PHP version: PHP 7.
Includes some code samples and references to the corresponding RFCs of each of the features.
1. PHP 7 – A look at the future
by Radu Murzea
25 July 2015
2. Agenda
A bit of History
Most important new features of PHP 7
Mini-demo of each one
Q&A
3. PHP – A bit of high-level history
PHP < 5
1995 - 2008
PHP with
most known
features
Zend
Engine 1
4. PHP – A bit of high-level history
PHP < 5
1995 - 2008 2004 - 2017
PHP 5
PHP with
most known
features
Zend
Engine 1
Zend
Engine 2
New
Object-Oriented
Model
5. PHP – A bit of high-level history
PHP < 5
1995 - 2008 2004 - 2017 2015 - ?
PHP 5 PHP 7
PHP with
most known
features
Zend
Engine 1
Zend
Engine 2
Zend
Engine 3
New
Object-Oriented
Model
Keep reading
6. AST-based Compilation
Separation of parser and compiler
Higher Maintainability
Decouple syntax issues from technical issues
Performance Improvement
Usually 10 – 20 % faster
But requires more memory
Removes many syntax limitations
See “Uniform Variable Syntax” Chapter
Reference
https://wiki.php.net/rfc/abstract_syntax_tree
AST
looks
like this
7. Uniform Variable Syntax
Consistent left-to-right variable dereferencing
Stuff like this is now possible:
$foo['bar']->baz->oof()::$rab
explode(‘|’, $x)[3]
$foo()[‘bar’]()
foo()()
(function() { return 1+2+3; })()
‘Foo’::$bar
Reference
https://wiki.php.net/rfc/uniform_variable_syntax
8. Return Type Declarations (I)
Motivation
Prevent unintended return values
Document return type in a way that is not easily removed (like phpdoc
comments)
Rules
In case of inheritance -> invariant enforcement
If return type is declared, NULL may not be returned
Not allowed on __construct(), __destruct() and __clone()
Multiple return types are NOT allowed
9. Return Type Declarations (II)
Reference
https://wiki.php.net/rfc/return_types
Example
class MyClass {
function a() { //return type is optional
return 123;
}
function b(): int { //fatal error - "int" doesn't exist
return 123;
}
function c(): ClassB { //fatal error - can't return null here
return null;
}
}
10. Anonymous Classes (I)
Motivation
Anonymous classes are frequently used in other languages (Java, C#)
Basic Rules
Instantiating requires providing values to constructor arguments
Inheritance and Traits works just like for named classes
Attempting to serialize will result in an ERROR
Use Cases
In very simple cases, where dedicated file + class-doc = overkill
When it’s small + you need it only once during execution
When you don’t want to hit the autoloader for extremely simple classes
Primitive support for situations where inner classes would make sense
Reference
https://wiki.php.net/rfc/anonymous_classes
11. Anonymous Classes (II)
Examples
$x = new class(123) {
public function __construct($a) {
$this->a = $a;
}
};
(new class extends SomeClass implements SomeInterface {
public function init() { /* ... */ }
})->doStuff();
class MyClass extends MyOtherClass {
public function getInstance() {
return new class implements MyInt { /* ... */ };
}
}
12. Many Fatal Errors become Exceptions
Motivation
Execution immediately aborted, cannot be recovered from
finally blocks or __destructor() s are not called
Solution
Reference
https://wiki.php.net/rfc/engine_exceptions_for_php7
https://wiki.php.net/rfc/throwable-interface
https://trowski.com/2015/06/24/throwable-exceptions-and-errors-in-php7/
13. Context Sensitive Lexer
Motivation
PHP reserved words prevent good/natural API designs
Example
This is now possible:
Finder::for(‘project’)
->where(‘name’)->like(‘%secret%’)
->and(‘priority’, ‘>’, 9)
->or(‘code’)->in([‘4’, ‘5’, ‘7’])
->and()->not(‘created_at’)->between([$t1, $t2])
->list($limit, $offset);
Reference
https://wiki.php.net/rfc/context_sensitive_lexer
14. Grouping Use Declarations (I)
Motivation
Cut verbosity when importing classes, functions or constants
Easier to identify which entities belong to the same module
Reference
https://wiki.php.net/rfc/group_use_declarations
Example
This:
use FooBarStuffA;
use FooBarStuffB as MyB;
becomes this:
use FooBar{
StuffA,
StuffB as MyB
};
15. Null Coalesce Operator
Motivation
Operations like “if data exists, use it; otherwise use default” are quite
cumbersome to do
Description/Examples
Denoted by ??
Returns result of 1st operand if it exists and is not NULL; otherwise
returns 2nd operand
The following 2 statements are equivalent:
$a = isset($_GET[‘a’]) ? $_GET[‘a’] : ‘default’;
$a = $_GET[‘a’] ?? ‘default’;
The following 2 statements are equivalent as well:
if (($a = A::$value) === null) { $a = $default; }
$a = A::$value ?? $default;
Reference
https://wiki.php.net/rfc/isset_ternary
16. Unicode Code Point Escape Syntax
Motivation
Proper escape syntax for Unicode characters
Support for more than 16-bit-length BPM characters
Syntax is u{xxxxxx} – with variable length
Examples
echo "u{202E}Right-to-left text";
will print: txet tfel-ot-thgiR
echo “u{1F602}”; //Emoji – Face with Tears of Joy
will print:
Reference
https://wiki.php.net/rfc/unicode_escape