Deploying PHP 7
Deploying PHP 7 PHP Meetup Dublin Jan.12, 2016 http://talks.php.net/dublin16 Rasmus Lerdorf @rasmus ✔ engine improvements 100%+ performance gain on most real-world applications Lower memory usage, sometimes drastically lower ✔ Persistent secondary file-based cache for OPCache ; --enable-opcache-file ; php.ini opcache.file_cache=/var/tmp ; php-cli.ini opcache.enable_cli=1 opcache.file_cache=/var/tmp opcache.file_cache_only=1 $ time composer >/dev/null real 0m0.040s user 0m0.032s sys 0m0.004s $ time composer >/dev/null real 0m0.019s user 0m0.016s sys 0m0.000s $ time php -d opcache.enable=0 /usr/local/bin/composer >/dev/null real 0m0.033s user 0m0.032s sys 0m0.000s /var/tmp ├── 7eeb6fe88104116c27c5650ddd83abf0 │ └── usr │ └── local │ └── bin │ └── composer.bin └── 7eeb6fe88104116c27c5650ddd83abf0phar: └── usr └── local └── bin └── composer ├── bin │ └── composer.bin ├── src │ ├── bootstrap.php.bin │ └── Composer │ ├── Command │ │ ├── AboutCommand.php.bin │ │ ├── ArchiveCommand.php.bin │ │ ├── ClearCacheCommand.php.bin │ │ ├── Command.php.bin │ │ ├── ConfigCommand.php.bin │ │ ├── CreateProjectCommand.php.bin │ │ ├── DependsCommand.php.bin │ │ ├── DiagnoseCommand.php.bin │ │ ├── DumpAutoloadCommand.php.bin │ │ ├── GlobalCommand.php.bin │ │ ├── Helper │ │ │ └── DialogHelper.php.bin │ │ ├── HomeCommand.php.bin │ │ ├── InitCommand.php.bin │ │ ├── InstallCommand.php.bin │ │ ├── LicensesCommand.php.bin │ │ ├── RemoveCommand.php.bin │ │ ├── RequireCommand.php.bin │ │ ├── RunScriptCommand.php.bin │ │ ├── SearchCommand.php.bin │ │ ├── SelfUpdateCommand.php.bin │ │ ├── ShowCommand.php.bin │ │ ├── StatusCommand.php.bin │ │ ├── UpdateCommand.php.bin │ │ └── ValidateCommand.php.bin │ ├── Composer.php.bin │ ├── Console │ │ └── Application.php.bin │ ├── Factory.php.bin │ ├── IO │ │ ├── BaseIO.php.bin │ │ ├── ConsoleIO.php.bin │ │ └── IOInterface.php.bin │ ├── Package │ │ ├── BasePackage.php.bin │ │ └── PackageInterface.php.bin │ ├── Script │ │ └── ScriptEvents.php.bin │ └── Util │ └── ErrorHandler.php.bin └── vendor ├── autoload.php.bin ├── composer │ ├── autoload_classmap.php.bin │ ├── autoload_namespaces.php.bin │ ├── autoload_psr4.php.bin │ ├── autoload_real.php.bin │ └── ClassLoader.php.bin └── symfony └── console └── Symfony └── Component └── Console ├── Application.php.bin ├── Command │ ├── Command.php.bin │ ├── HelpCommand.php.bin │ └── ListCommand.php.bin ├── Descriptor │ ├── ApplicationDescription.php.bin │ ├── DescriptorInterface.php.bin │ ├── Descriptor.php.bin │ ├── JsonDescriptor.php.bin │ ├── MarkdownDescriptor.php.bin │ ├── TextDescriptor.php.bin │ └── XmlDescriptor.php.bin ├── Formatter │ ├── OutputFormatterInterface.php.bin │ ├── OutputFormatter.php.bin │ ├── OutputFormatterStyleInterface.php.bin │ ├── OutputFormatterStyle.php.bin │ └── OutputFormatterStyleStack.php.bin ├── Helper │ ├── DebugFormatterHelper.php.bin │ ├── DescriptorHelper.php.bin │ ├── DialogHelper.php.bin │ ├── FormatterHelper.php.bin │ ├── HelperInterface.php.bin │ ├── Helper.php.bin │ ├── HelperSet.php.bin │ ├── InputAwareHelper.php.bin │ ├── ProcessHelper.php.bin │ ├── ProgressHelper.php.bin │ ├── QuestionHelper.php.bin │ ├── TableHelper.php.bin │ ├── Table.php.bin │ └── TableStyle.php.bin ├── Input │ ├── ArgvInput.php.bin │ ├── ArrayInput.php.bin │ ├── InputArgument.php.bin │ ├── InputAwareInterface.php.bin │ ├── InputDefinition.php.bin │ ├── InputInterface.php.bin │ ├── InputOption.php.bin │ └── Input.php.bin └── Output ├── ConsoleOutputInterface.php.bin ├── ConsoleOutput.php.bin ├── NullOutput.php.bin ├── OutputInterface.php.bin ├── Output.php.bin └── StreamOutput.php.bin 32 directories, 87 files ✔ Abstract Syntax Tree!! echo substr("abc", [1,2]); % phan -a test.php AST_STMT_LIST @ 1 0: AST_STMT_LIST @ 2 0: AST_ECHO @ 2 0: AST_CALL @ 2 0: AST_NAME @ 2 flags: NAME_NOT_FQ (1) 0: "substr" 1: AST_ARG_LIST @ 2 0: "abc" 1: AST_ARRAY @ 2 0: AST_ARRAY_ELEM @ 2 flags: 0 0: 1 1: null 1: AST_ARRAY_ELEM @ 2 flags: 0 0: 2 1: null % phan test.php test.php:2 TypeError arg#2(start) is int[] but substr() takes int ✔ Exceptions on Fatals function call_method($obj) { $obj->method(); } call_method(null); Fatal error: Uncaught Error: Call to a member function method() on null in file:2 Stack trace: #0 file(4): call_method(NULL) #1 {main} thrown in file on line 2 try { call_method(null); } catch (Error $e) { echo "Caught Exception: {$e->getMessage()}\n"; } Caught Exception: Call to a member function method() on null PHP 7 Exception Hierarchy Throwable ➥ Exception implements Throwable ➥ Error implements Throwable ➥ TypeError extends Error ➥ ParseError extends Error ✔ Return Types function get_config(): array { return 42; } get_config(); Fatal error: Uncaught TypeError: Return value of get_config() must be of the type array, integer returned in file:2 Stack trace: #0 file(4): get_config() #1 {main} thrown in file on line 2 ✔ Coercive Scalar Types function logmsg(string $msg, int $level, float $severity) { var_dump($msg); // string(1) "1" var_dump($level); // int(2) var_dump($severity); // float(3) } logmsg(1, "2.5 bananas", 3); Notice: A non well formed numeric value encountered in file on line 2 ✔ Strict Scalar Types declare(strict_types=1); ... logmsg(1, "2.5", 3); Fatal error: Uncaught TypeError: Argument 1 passed to logmsg() must be of the type string, integer given, called in file on line 7 and defined in file:3 Stack trace: #0 file(7): logmsg(1, '2.5', 3) #1 {main} thrown in file on line 2 ✔ Anonymous Classes return new class($controller) implements Page { public function __construct($controller) { /* ... */ } /* ... */ }; class MyObject extends MyStuff { public function getInterface() { return new class implements MyInterface { /* ... */ }; } } ✔ Coalesce Operator $a = NULL; $b = 0; $c = 2; echo $a ?? $b; // 0 echo $c ?? $b; // 2 echo $a ?? $b ?? $c; // 0 echo $a ?? $x ?? $c; // 2 ✔ Spaceship Operator |=| Tie Fighter k=k Tie Interceptor <==> Tie Bomber <=> Tie Advanced X1 ✔ function cmp_php5($a, $b) { return ($a < $b) ? -1 : (($a >$b) ?
1 : 0); } function cmp_php7($a, $b) { return $a <=> $b; } ✔ Zero-cost Assertions function test($arg) { assert($arg > 20 && $arg < 110, "$arg is invalid"); } ini_set('zend.assertions',0); test(16); ini_set('zend.assertions',1); test(17); ini_set('assert.exception',1); test(18); Warning: assert(): 17 is invalid failed in file on line 2 Fatal error: Uncaught AssertionError: 18 is invalid in file:2 Stack trace: #0 file(2): assert(false, '18 is invalid') #1 file(6): test(18) #2 {main} thrown in file on line 2 ; Completely skip compiling assert() calls ; (can only be set in php.ini) zend.assertions = -1 ✔ Added Closure::call() $f = function () { return $this->n; }; class MyClass { private $n = 42; } $myC = new MyClass; $c = $f->bindTo($myC, "MyClass"); $c(); $f = function () { return $this->n; }; class MyClass { private $n = 42; } $myC = new MyClass; $f->call($myC); ✔ Removal of many deprecated features (Your PHP4 code will break!) - ext/ereg (use ext/pcre instead) - preg_replace() eval modi
The rest of this page is on the live site.