Last active
May 15, 2018 05:21
-
-
Save trungx/0a3bf900799aa6fdc0fde6393163f475 to your computer and use it in GitHub Desktop.
the new feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Scalar type declarations | |
| */ | |
| //declare(strict_types=1); | |
| function add(int $a, int $b){ | |
| return $a + $b; | |
| } | |
| var_dump(add(1,2)); | |
| var_dump(add("1","2")); | |
| /** | |
| * Anonymous classes | |
| */ | |
| /* $foo = new Class{ | |
| public function foo(){ | |
| return "bar"; | |
| } | |
| }; | |
| var_dump($foo,$foo->foo());*/ | |
| /** | |
| * Closure::call() | |
| */ | |
| /* class Foo{ | |
| private $foo = 'bar'; | |
| } | |
| $getFooCallBack = function(){ | |
| return $this->foo; | |
| }; | |
| //PHP5 style | |
| $binding = $getFooCallBack->bindTo(new Foo,'Foo'); | |
| echo $binding().PHP_EOL; | |
| //PHP7 style | |
| echo $getFooCallBack->call(new Foo).PHP_EOL;*/ | |
| /** | |
| * Generator delegation | |
| */ | |
| /*function gen(){ | |
| yield 1; | |
| yield 2; | |
| yield from gen2(); | |
| } | |
| function gen2(){ | |
| yield 3; | |
| yield 4; | |
| } | |
| foreach(gen() as $val){ | |
| echo $val, PHP_EOL; | |
| }*/ | |
| /** | |
| * Generator return expressions | |
| * | |
| */ | |
| /*$gen = (function(){ | |
| yield 1; | |
| yield 2; | |
| return 3; | |
| })(); | |
| foreach($gen as $val){ | |
| echo $val, PHP_EOL; | |
| } | |
| echo $gen->getReturn(), PHP_EOL;*/ | |
| /** | |
| * NULL coalesce operator | |
| */ | |
| /*$array = ['foo'=>'bar']; | |
| //PHP5 style | |
| $message = isset($array['foo'])? $array['foo'] : 'not set'; | |
| echo $message.PHP_EOL; | |
| //PHP7 style | |
| $message = $array['foo'] ?? 'not set'; | |
| echo $message.PHP_EOL;*/ | |
| /** | |
| * Space ship operator | |
| */ | |
| /*$array = [ | |
| "1 <=> 1" => 1 <=> 1, | |
| "1 <=> 2" => 1 <=> 2, | |
| "2 <=> 1" => 2 <=> 1 | |
| ]; | |
| var_dump($array);*/ | |
| /** | |
| * Throwable interface | |
| */ | |
| /*try{ | |
| sqdf(); | |
| }catch(Throwable $t){ | |
| echo "Throwable: ".$t->getMessage().PHP_EOL; | |
| } | |
| //Exception as Throwable | |
| try{ | |
| throw new Exception('Bla'); | |
| }catch(Throwable $t){ | |
| echo "Throwable: ".$t->getMessage().PHP_EOL; | |
| } | |
| //Error | |
| try{ | |
| sqdf(); | |
| }catch(Throwable $t){ | |
| echo "Error: ".$t->getMessage().PHP_EOL; | |
| }catch(Exception $e){ | |
| echo "Exception: ".$t->getMessage().PHP_EOL; | |
| } | |
| //Exception | |
| // try{ | |
| // }catch()*/ | |
| /** | |
| * Dirname levels | |
| */ | |
| /*echo dirname('usr/local/bin').PHP_EOL; | |
| echo dirname('usr/local/bin',1).PHP_EOL; | |
| echo dirname('usr/local/bin',2).PHP_EOL; | |
| echo dirname('usr/local/bin',3).PHP_EOL;*/ | |
| /** | |
| * Int division | |
| */ | |
| /*var_dump( | |
| intdiv(10,3), | |
| (10/3) | |
| );*/ | |
| /** | |
| * Uniform variable syntax | |
| */ | |
| /*Expression | |
| $$foo['bar']['baz'] | |
| $foo->$bar['baz'] | |
| $foo->$bar['baz']() | |
| Foo::$bar['bar']() | |
| PHP5 interpretation | |
| ${$foo['bar']['baz']} | |
| $foo->{$bar['baz']} | |
| $foo->{$bar['baz']}() | |
| Foo::{$bar['baz']}() | |
| PHP7 interpretation | |
| ($$foo)['bar']['baz'] | |
| ($foo->$bar)['baz'] | |
| ($foo->$bar)['baz']() | |
| (Foo::$bar)['baz']() | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment