Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP7: Hello World!

1,410 views

Published on

My presentation for BulgariaPHP UG 21.11.2015

Published in: Software
  • Be the first to comment

  • Be the first to like this

PHP7: Hello World!

  1. 1. Pavel Nikolov PHP7: Hello World!
  2. 2. Who The Fuck Is This Guy? Pavel Nikolov
  3. 3. Pavel Nikolov @pavkatar
 CEO at ApiHawk Technologies, Geek, API Evangelist. iLover, Developer, Coffee drinker and just an irregular person Sofia, Bulgaria
  4. 4. Hello World!
  5. 5. Version Release Lifetime Personal Homepage Tools (PHP tools) v.1 June 8, 1995 ~ 2 years PHPFI (Form Interpreter) v2 November 1997 ~ 1 year PHP3 June 1998 ~ 2 years PHP4 May 2000 ~ 4 years PHP5 July 2004 ~ 11 years PHP5.3 (PHP 6 - Unicode) June 2009 PHP7 Fall 2015 2015 - 20 Years of PHP
  6. 6. PHP7 CHANGES “UNDER THE HOOD”
  7. 7. PHP Version 5.3 5.4 5.5 5.6 7 Interations per second 282544 278205 304330 301689 583975 288611 268948 307818 308273 603583 288517 279900 296669 301989 606468 282384 282060 299921 308735 596079 288162 280692 298747 308003 610696 286300 278374 307043 303139 594547 291027 278703 305950 311487 602184 292292 282226 296287 312770 610380 Average 287480 278639 302096 307011 600989 Percentage faster than previous version N/A -3.08% 8.42% 1.63% 95.76% Executor: Faster
  8. 8. Executor: Less Memory 32 bit 64 bit PHP 5.6 7.37 MiB 13.97 MiB PHP 7.0 3.00 MiB 4.00 MiB $startMemory = memory_get_usage(); $array = range(1, 100000); echo memory_get_usage() - $startMemory, " bytesn";
  9. 9. PHP7 CHANGES “UNDER THE HOOD” • Executor: Faster • Executor: Less memory • Compiler: Generates better bytecode
  10. 10. PHP7 CHANGES “UNDER THE HOOD” • Executor: Faster • Executor: Less memory • Compiler: Generates better bytecode • Parser: Now based on AST (abstract syntax tree)
  11. 11. PHP7 CHANGES “UNDER THE HOOD” • Executor: Faster • Executor: Less memory • Compiler: Generates better bytecode • Parser: Now based on AST (abstract syntax tree) • Lexer: Now contex-sensitive with support for semi-reserved words class Collection { public function forEach(callable $callback) { /* */ } public function list() { /* */ } }
  12. 12. PHP7 CHANGES “UNDER THE HOOD” • Executor: Faster • Executor: Less memory • Compiler: Generates better bytecode • Parser: Now based on AST (abstract syntax tree) • Lexer: Now contex-sensitive with support for semi-reserved words PHP5.6: PHP Parse error: Syntax error, unexpected T_FOREACH, expecting T_STRING on line 2 PHP Parse error: Syntax error, unexpected T_LIST, expecting T_STRING on line 3
  13. 13. callable class trait extends implements static abstract final public protected private const enddeclare endfor endforeach endif endwhile and global goto instanceof insteadof interface namespace new or xor try use var exit list clone include include_once throw array print echo require require_once return else elseif default break continue switch yield function if endswitch finally for foreach declare case do while as catch die self parent This is a list of currently globally reserved words that will become semi-reserved in case proposed change gets approved:
  14. 14. PHP7 CHANGES “UNDER THE HOOD” Limitation! It's still forbidden to define a class constant named as class because of the class name resolution ::class: In practice, it means that we would drop from 64 to only 1 reserved word that affects only class constant names. class Foo { const class = 'Foo'; // Fatal error }   // Fatal error: Cannot redefine class constant Foo::CLASS as it is reserved in %s on line %d
  15. 15. New Features http://php.net/manual/en/migration70.new-features.php
  16. 16. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); function sumOfInts(int $int, bool $bool, string $string){}
  17. 17. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types=1); • Return type declarations === type_declarations
  18. 18. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); • Return type declarations === type_declarations • Null coalesce operator $username = $_GET['user'] ?? 'nobody'; // This is equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
  19. 19. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); • Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=>
  20. 20. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); • Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() <?php define('ANIMALS', [     'dog',     'cat',     'bird' ]); echo ANIMALS[1]; // outputs "cat" ?>
  21. 21. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); • Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes $var = new class implements Logger {     public function log(string $msg) {         echo $msg;     } };
  22. 22. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); • Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() // converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2 $data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]);
  23. 23. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); • Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() • Group use declarations use somenamespace{ClassA, ClassB, ClassC as C};
  24. 24. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); • Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() • Group use declarations • Integer division with intdiv()
  25. 25. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); • Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() • Group use declarations • Integer division with intdiv() • Fatal Errors to Exceptions
  26. 26. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); • Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() • Group use declarations • Integer division with intdiv() • Fatal Errors to Exceptions • Removal of date.timezone Warning
  27. 27. PHP7 NEW FEATURES • Type declarations: bool, float, int, string i)coercively ii)strictly - declare(strict_types); • Return type declarations === type_declarations • Null coalesce operator • Spaceship operator <=> • Constant arrays using define() • Anonymous classes • Filtered unserialize() • Group use declarations • Integer division with intdiv() • Fatal Errors to Exceptions • Removal of date.timezone Warning • Opcache: opcache.huge_code_pages=1 && opcache.file_cache
  28. 28. backward compatibility breakshttp://php.net/manual/en/migration70.incompatible.php
  29. 29. Old and new evaluation of indirect expressions Expression PHP 5 interpretation PHP 7 interpretation $$foo['bar']['baz'] ${$foo['bar']['baz']} ($$foo)['bar']['baz'] $foo->$bar['baz'] $foo->{$bar['baz']} ($foo->$bar)['baz'] $foo->$bar['baz']() $foo->{$bar['baz']}() ($foo->$bar)['baz']() Foo::$bar['baz']() Foo::{$bar['baz']}() (Foo::$bar)['baz']()
  30. 30. foreach no longer a the internal array pointer <?php $array = [0, 1, 2]; foreach ($array as &$val) {     var_dump(current($array)); } ?> Output of the above example in PHP 5: int(1) int(2) bool(false) Output of the above example in PHP 7: int(0) int(0) int(0)
  31. 31. foreach by-reference has improved iteration behaviour ¶ Output of the above example in PHP 5: int(0) Output of the above example in PHP 7: int(0) int(1) <?php $array = [0]; foreach ($array as &$val) {     var_dump($val);     $array[1] = 1; } ?>
  32. 32. Changes to Division By Zero Output of the above example in PHP 5: Warning: Division by zero in %s on line %d bool(false) Warning: Division by zero in %s on line %d bool(false) Warning: Division by zero in %s on line %d bool(false) <?php var_dump(3/0); var_dump(0/0); var_dump(0%0); ?> Output of the above example in PHP 7: Warning: Division by zero in %s on line %d float(INF) Warning: Division by zero in %s on line %d float(NAN) PHP Fatal error: Uncaught DivisionByZeroError: Modulo by zero in %s line %d
  33. 33. $HTTP_RAW_POST_DATA removed $HTTP_RAW_POST_DATA is no longer available. The php://input stream should be used instead.
  34. 34. JSON extension replaced with JSOND ¶ The JSON extension has been replaced with JSOND, causing two minor BC breaks. Firstly, a number must not end in a decimal point (i.e. 34. must be changed to either 34.0 or 34). Secondly, when using scientific notation, the e exponent must not immediately follow a decimal point (i.e. 3.e3 must be changed to either 3.0e3 or 3e3).
  35. 35. Compatibility Check https://github.com/sstalle/php7cc
  36. 36. https://hub.docker.com/_/php/
  37. 37. TALK/SMOKE TIME

×