際際滷

際際滷Share a Scribd company logo
New in PHP 7
Conference Confirmed
http://truenorthphp.ca
November 5, 6, 7
@TrueNorthPHP
Talk to Pete or Vic
about sponsorship
opportunities. Call for
speakers and tickets
coming soon.
http://gtaphp.org/ks
New in php 7
O¨Reilly has offered our group 50% off all e-Books,
other items also discounted. Discount code: PCBW
? Ask questions at any time
? Point out mistakes at any time
? 際際滷s will be posted online so there is no need to
photograph slides
Roadmap
? New Features
? Backwards Compatibility Breaks
? Bug Fixes
? Wrap-up
? ^Get the most ̄ tips as we go
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.
New Features
Faster than PHP 7
Based on phpng
Many additional optimizations
Still getting considerably faster
Should rival hack for performance
Return types
function foo(): array {
return [];
}
function foo(): DateTime {
return null; // invalid
}
Return types
Class constructors, destructors and clone
methods may not declare return types.
No void return type
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)
Scalar Type Declarations
function add(int $a, int $b) {
return $a + $b;
}
echo add(1, 2); // 3
echo add(1.1, 2); // 3
Scalar Type Declarations
declare(strict_types=1);
function add(int $a, int $b) {
return $a + $b;
}
echo add(1, 2); // 3
echo add(1.1, 2); // Error
1.0 would also throw an error,
but coercing an int to a float is ok.
<=>
function order_func($a, $b) {
return $a <=> $b;
}
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
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:
Generator Return Values
function foo() {
yield 1;
yield 2;
return 42;
}
$bar = foo();
foreach ($bar as $element) {
echo $element, "n";
}
var_dump($bar->getReturn());
1
2
int(42)
Generator Delegationfunction myGeneratorFunction($foo) {
// ... do some stuff with $foo ...
$bar = yield from factoredComputation1($foo);
// ... do some stuff with $bar ...
$baz = yield from factoredComputation2($bar);
return $baz;
}
function factoredComputation1($foo) {
yield ...; // pseudo-code (something we factored out)
yield ...; // pseudo-code (something we factored out)
return 'zanzibar';
}
function factoredComputation2($bar) {
yield ...; // pseudo-code (something we factored out)
yield ...; // pseudo-code (something we factored out)
return 42;
Engine Exceptions
try {
$o = null;
$o->method();
} catch (EngineException $e) {
echo "Exception: {$e->getMessage()}n";
}
try {
eval("Hello GTA-PHP!");
} catch (ParseException $e) {
echo "Exception: {$e->getMessage()}n";
}
Engine Exceptions
BaseException (abstract)
EngineException
ParseException
Exception
ErrorException
RuntimeException
calls to a member function of a non-object now raise a non fatal E
Group Use Declarations
use FooLibraryBarBaz{ ClassA, ClassB, ClassC, ClassD as Fizbo };
use SymfonyComponentConsole{
HelperTable,
InputArrayInput,
InputInputInterface,
OutputNullOutput,
OutputOutputInterface,
QuestionQuestion,
QuestionChoiceQuestion as Choice,
QuestionConfirmationQuestion,
};
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()()
// 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
Unicode Escape Syntax
echo "We u{2665} PHPn";
We ? PHP
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"];
Closure::call
class Foo {
private $x = 3;
}
$foo = new Foo;
$foobar = function () {
var_dump($this->x);
};
$foobar->call($foo); // prints int(3)
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]);
Small Stuff
Turn gc_collect_cycles into
function pointer
Remove the
date.timezone warning
Remove E_STRICT in favour
of E_DEPRECATED,
E_NOTICE, E_WARNING or
removal
Continue output
buffering despite
aborted connection
^Fixed ̄ assert()
Better i18n/l10n with
IntlChar class
Integer Semantics
intdiv(3, 2); //1
session_start() read_only and lazy_write options
Backwards
Compatibility Breaks
Reserved words: int, float, bool,
string, true, false, null
Reserved for future use: resource,
object, scalar, mixed, numeric
Constructor behaviour of internal classes:
what returned null now throws an error
"Remove" PHP 4 constructors
with E_DEPRECATED
Much that was deprecated is
now GONE - the big one is mysql
Extension API: Big changes, possible ^PNI ̄
Custom session handlers will
return true or false, not
0 (SUCCESS) or 1
(FAILURE)
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
Bottom line: Modern PHP should work fine
Bug Fixes
foreach
$a = [1,2,3];
foreach($a as $v) {
echo $v . " - " .
current($a) . "n";
}
1 - 2
2 - 2
3 - 2
$a = [1,2,3];
$b = $a;
foreach($a as $v) {
echo $v . " - " .
current($a) . "n";
}
1 - 1
2 - 1
3 - 1
PHP 7¨s foreach doen¨t change
an array¨s internal pointer
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
Multiple Default Cases in a
Switch
switch ($expr) {
default:
doSomething();
break;
default:
somethingElse();
}
Previously called last default
PHP 7 raises
E_COMPILE_ERROR
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
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
Thank-you!

More Related Content

New in php 7

Editor's Notes

  • #8: PHP 5.7 vote: https://wiki.php.net/rfc/php57 PHP6 vs PHP7 vote: https://wiki.php.net/rfc/php6
  • #11: https://wiki.php.net/rfc/return_types
  • #15: declare(strict_types=1); must be at the top and applies to the current file.
  • #16: https://wiki.php.net/rfc/combined-comparison-operator
  • #17: https://wiki.php.net/rfc/anonymous_classes
  • #18: https://wiki.php.net/rfc/generator-return-expressions
  • #19: https://wiki.php.net/rfc/generator-delegation
  • #21: New ^Pokemon ̄ Exception class if you really want to ^catch them all ̄. https://wiki.php.net/rfc/catchable-call-to-member-of-non-object
  • #22: 2nd format better for version control. Also, note the trailing , as in array syntax.
  • #23: https://wiki.php.net/rfc/uniform_variable_syntax
  • #25: https://wiki.php.net/rfc/unicode_escape
  • #27: https://wiki.php.net/rfc/closure_apply
  • #28: https://wiki.php.net/rfc/secure_unserialize
  • #30: https://wiki.php.net/rfc/gc_fn_pointer - Allow profilers to take time in garbage collection into account
  • #31: https://wiki.php.net/rfc/date.timezone_warning_removal
  • #32: https://wiki.php.net/rfc/reclassify_e_strict
  • #33: https://wiki.php.net/rfc/continue_ob
  • #34: https://wiki.php.net/rfc/expectations - The new assert()
  • #35: https://wiki.php.net/rfc/intl.char
  • #36: https://wiki.php.net/rfc/integer_semantics - NaN, Infinite, shifting bits out of range - no longer platform dependant
  • #37: https://wiki.php.net/rfc/intdiv - Sadly no %% operator
  • #38: https://wiki.php.net/rfc/session-lock-ini
  • #40: https://wiki.php.net/rfc/reserve_more_types_in_php_7
  • #41: https://wiki.php.net/rfc/internal_constructor_behaviour
  • #42: https://wiki.php.net/rfc/remove_php4_constructors
  • #45: https://wiki.php.net/rfc/session.user.return-value
  • #47: https://wiki.php.net/rfc/session.user.return-value
  • #49: https://wiki.php.net/rfc/php7_foreach
  • #50: https://wiki.php.net/rfc/php7_foreach
  • #51: https://wiki.php.net/rfc/php7_foreach
  • #52: https://wiki.php.net/rfc/fix_list_behavior_inconsistency