際際滷s from the GTA-PHP meetup about the new features in PHP 7. 際際滷s had corresponding RFC pages linked to them in the speaker notes, but they don't seem to correspond to pages here so I've made the original keynote file available at http://gtaphp.org/presentations/NewInPHP7.zip and a PowerPoint version at http://gtaphp.org/presentations/NewInPHP7.pptx.
4. O¨Reilly has offered our group 50% off all e-Books,
other items also discounted. Discount code: PCBW
5. ? Ask questions at any time
? Point out mistakes at any time
? 際際滷s will be posted online so there is no need to
photograph slides
6. Roadmap
? New Features
? Backwards Compatibility Breaks
? Bug Fixes
? Wrap-up
? ^Get the most ̄ tips as we go
7. 6.0?
5.7?
Used with permission from https://www.flickr.com/photos/jeepersmedia/8030074932/.
"PHP 7" on hat is a modification from the original image.
12. Scalar Type Declarations
int, float, string and bool
^an atomic quantity that can hold
only one value at a time ̄
- Wikipedia
integer or boolean
declare(strict_types=1)
16. Anonymous classes
class MyLogger {
public function log($msg) {
print_r($msg . "n");
}
}
$pusher->setLogger( new MyLogger() );
$pusher->setLogger(new class {
public function log($msg) {
print_r($msg . "n");
}
});
Old:
New:
21. Group Use Declarations
use FooLibraryBarBaz{ ClassA, ClassB, ClassC, ClassD as Fizbo };
use SymfonyComponentConsole{
HelperTable,
InputArrayInput,
InputInputInterface,
OutputNullOutput,
OutputOutputInterface,
QuestionQuestion,
QuestionChoiceQuestion as Choice,
QuestionConfirmationQuestion,
};
22. Uniform Variable Syntax
// support missing combinations of operations
$foo()['bar']()
[$obj1, $obj2][0]->prop
getStr(){0}
// support nested ::
$foo['bar']::$baz
$foo::$bar::$baz
$foo->bar()::baz()
// support nested ()
foo()()
$foo->bar()()
Foo::bar()()
$foo()()
23. // support operations on arbitrary (...) expressions
(...)['foo']
(...)->foo
(...)->foo()
(...)::$foo
(...)::foo()
(...)()
// two more practical examples for the last point
(function() { ... })()
($obj->closure)()
// support all operations on dereferencable scalars (not very
useful)
"string"->toLower()
[$obj, 'method']()
'Foo'::$bar
Uniform Variable Syntax
25. Coalesce ??
isset($_GET['user']) ? $_GET['user'] : 'nobody'
$_GET['user'] ?? 'nobody'
^COALESCE, an SQL command that selects
the first non-null from a range of values ̄
- Wikipedia
$x = ["yarr" => "meaningful_value"];
echo $x["aharr"] ?? $x["waharr"] ?? $x["yarr"];
26. Closure::call
class Foo {
private $x = 3;
}
$foo = new Foo;
$foobar = function () {
var_dump($this->x);
};
$foobar->call($foo); // prints int(3)
27. Filtered unserialize()
// Unserialize everything as before
$data = unserialize($foo);
// Convert all objects into __PHP_Incomplete_Class object
$data = unserialize($foo, ["allowed_classes" => false]);
//Convert all objects except MyClass / MyClass2 into __PHP_Incomplete_Class
$data = unserialize($foo, ["allowed_classes" => [
"MyClass",
^MyClass2",
]);
//accept all classes as in default
$data = unserialize($foo, ["allowed_classes" => true]);
45. Remove alternative PHP tags
<% opening tag
<%= opening tag with echo
%> closing tag
(<scripts+languages*=s*(php|"php"|'php')s*>)i opening tag
(</script>)i closing tag
ot remove short opening tags (<?) or short opening tags with echo
49. Remove hex support in
numeric strings
$str = '0x123';
if (!is_numeric($str)) {
throw new Exception('Not a number');
}
$n = (int) $str;
// Exception not thrown, instead the
// wrong result is generated here:
// 0
50. Multiple Default Cases in a
Switch
switch ($expr) {
default:
doSomething();
break;
default:
somethingElse();
}
Previously called last default
PHP 7 raises
E_COMPILE_ERROR
51. Fix list() Inconsistency
list($a,$b) = "aa";
var_dump($a,$b);
$a[0]="ab";
list($a,$b) = $a[0];
var_dump($a,$b);
NULL
NULL
string(1) "a"
string(1) "b"
PHP 7¨s list treats
strings like arrays
52. References
? PHP 7 at a glance:
http://devzone.zend.com/4693/php-7-glance/
? What to Expect When You¨re Expecting PHP 7:
https://blog.engineyard.com/2015/what-to-expect-
php-7
? https://wiki.php.net/rfc#php_70
https://wiki.php.net/rfc#pending_implementation