PHP Göteborg, Meetup #3, 2016-02-25
Rickard Bennison
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
$username = $_GET['user'] ?? 'nobody';
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
use FooLibrary\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD as Fizbo };
use FooLibrary\Bar\Baz\ClassA;
use FooLibrary\Bar\Baz\ClassB;
use FooLibrary\Bar\Baz\ClassC;
use FooLibrary\Bar\Baz\ClassD as Fizbo;
function call_method($obj) {
$obj->method();
}
call_method(null);
// Fatal error: Call to a member function method() on a non-object
try {
call_method(null); // oops!
} catch (EngineException $e) {
echo "Exception: {$e->getMessage()}\n";
}
// Exception: Call to a member function method() on a non-object
$pusher->setLogger(new class {
public function log($msg) {
print_r($msg . "\n");
}
});
class Foo { private $x = 3; }
$foo = new Foo;
$foobar = function () { var_dump($this->x); };
$foobar->call($foo); // prints int(3)
$bound = $foobar->bindTo($foo, $foo);
$bound(); // prints int(3)
switch ($type) {
case "foo": …
case "bar": …
default:
assert(false, 'Unknown type ' . $type);
}
# zero cost for production
zend.assertions = -1
session_start([
'read_and_close' => true, // Read and then close the session lock
]);
# Only write when session data changes
session.lazy_write = 1
string random_bytes ( int $length )
int random_int ( int $min , int $max )
Reduced memory consumption
Redured memory management overhead
Researched JIT compilation
Extensions need to be updated to support phpng
int
, float
, string
and bool
can now be typehinted
<?php
function add(float $a, float $b): float {
return $a + $b;
}
add("1", "2"); // float(3)
<?php
declare(strict_types=1);
function add(float $a, float $b): float {
return $a + $b;
}
add("1", "2");
// Catchable fatal error: Argument 1 passed to add() must be of the type integer
function foo(): array {
return [];
}
"Uniform Variable Syntax"
All deprecated functionality removed
Some errors changed to Exceptions
E_STRICT removed and moved to other levels
list()
with strings is unsupported
Division by zero returns INF
/NAN
instead of false
Scalar names can no longer be used as class names
foreach
is now less quirky
Strings containing hexadecimal numbers are no longer considered to be numeric
Calls from incompatible context removed
Functions cannot have multiple parameters with the same name
Switch statements cannot have multiple default blocks
<script language="php">echo 'no longer works';</script>