On Habré, there was already a translation with a review a few months ago, but the first release candidate PHP 7.1 recently came out, which means there will be no more significant changes and you can tell what exactly the changes will be in the release. I decided to revive a little dry “changelog” with my free translation of the changes that the new minor version of 7.x branches will bring us.function someNethod(): void { // return // return; // return null; // return 123; } function bar(void $foo) {} // : Fatal error: void cannot be used as a parameter type in.... function walkList(iterable $list): iterable { foreach ($list as $value) { yield $value['id']; } } function callMethod(?Bar $bar): ?Bar {} $this->callMethod($bar); // $this->callMethod(null); // $this->callMethod(); // function callMethod(int $bar = null) {} $this->callMethod(1); // $this->callMethod(null); // $this->callMethod(); // function callMethod(?Bar $bar = null) {} // “?” interface Fooable { function foo(int $i): ?Fooable; } interface StrictFooable extends Fooable { function foo(?int $i): Fooable; // valid } echo $msg[-1]; // echo $msg{-3}; // RFC $str{} $str[] . ["test" => $a, "name" => $b] = ["name" => "Hello", "test" => "World!"]; var_dump($a); // World! var_dump($b); // Hello // Parse error: syntax error, ... ["a" => $a, $b] = ["a" => 1, 2] // Parse error: syntax error, ... list(,,,, "key" => $keyed) = $array; $points = [ ["x" => 1, "y" => 2], ["x" => 2, "y" => 1] ]; [["x" => $x1, "y" => $y1], ["x" => $x2, "y" => $y2]] = $points; Closure::fromCallable(callable $calback); class A { public function getValidator(string $name = 'byDefault') { return Closure::fromCallable([$this, $name]); } private function byDefault(...$options) { echo "Private default with:".print_r($options, true); } public function __call ( string $name , array $args ) { echo "Call $name with:".print_r($args, true); } } $a = new A(); $a->getValidator("test")(1,2,3); // Call test with: Array ( [0] => 1 [1] => 2 [2] => 3 ) $a->getValidator()('p1', 'p2'); // Private default with: Array ( [0] => 'p1', [1] => 'p2') // Closure::fromCallable ($this) , // return [$this, $name]; $a->getValidator()('p1', 'p2'); // // Call byDefault with:Array ( [0] => p1 [1] => p2 ) // class Token { // “public” const PUBLIC_CONST = 0; // private const PRIVATE_CONST = 0; protected const PROTECTED_CONST = 0; public const PUBLIC_CONST_TWO = 0; // private const FOO = 1, BAR = 2; } try { echo "OK"; } catch (Exception | DomainException $e) { // ... 2 } catch (TypeError $e) { // ... } $numberOfApples = "10 apples" + "5 pears"; // // Notice: A non well formed numeric string encountered in example.php on line 3 // Notice: A non well formed numeric string encountered in example.php on line 3 $numberOfPears = 5 * "orange"; // Warning: A non-numeric string encountered in example.php on line 3 function foo($a) { var_dump($a); // $a NULL } foo(); // Fatal error: Uncaught ArgumentCountError: Too few arguments to function foo(), 0 passed in... Speed to: Requests per second: 899.36 [# / sec] Speed after: Requests per second: 2278.59 [# / sec]
function foo($this) { // Fatal error: Cannot use $this as parameter } static $this; // Fatal error: Cannot use $this as static variable global $this; // Fatal error: Cannot use $this as global variable try { ... } catch (Exception $this) { // Fatal error: Cannot re-assign $this } foreach ($a as $this) { // Fatal error: Cannot re-assign $this } unset($this); // Fatal error: Cannot unset $this $a = "this"; $$a = 42; // throw new Error("Cannot re-assign $this") function foo() { var_dump($this); // throws "Using $this when not in object context" // php-7.0 emitted "Undefined variable: this" and printed NULL } foo(); // Source: https://habr.com/ru/post/309858/
All Articles