PHP 7 - files.meetup.comfiles.meetup.com/120882/PHP7-new-pdf.pdf · SMP Debian 3.16.7-ckt9-2...
Transcript of PHP 7 - files.meetup.comfiles.meetup.com/120882/PHP7-new-pdf.pdf · SMP Debian 3.16.7-ckt9-2...
PHP 7 Impactful Changes
Patrick Devins• Developer/Architect at American Cable and
Telephone
• 4 yrs. PHP Development | 10+ yrs. in the Industry (Server Management, Sales, Service, Operations, etc.)
• 6+ yrs. Experience varying levels of management
• Airboat enjoyer, bad fisherman, and outdoorsman.
• Ignored on Twitter @pddevins, lurking on Instagram as thepoddster, rarely on LinkedIn and too old for Facebook as myself.
PHP 7: Context
• What is PHP and why should I consider building my next project with it?
• Why PHP 7 and not PHP 6?
• PHP 7 originates from PHPNG (PHP NextGeneration)
• More performant
• Complete compatibility with the original Zend Engine
• “Is this talk for me?”
PHP 7
Timeline
Scalar Types
Existing types • array : array(), […] • <Objects> : stdClass, Exception, Iterator • callable : lambda functions, __invoke(), class/method
Adds new types
• int : Integers; 1, 2, 3, etc. (integer alias has been removed)
• float : Floats/Decimals; 5, 100.32, 3.141592
• bool : Boolean; true, false (boolean alias has been removed)
• string : Strings; ‘Patrick’, ‘active’, ‘company vehicle’
Scalar Type Hints and Return Types
• Introduces Strict and Coercive modes
• declare(strict_types=1); (value can be 1 or 0)
• Defaults to Coercive mode (existing behavior)
• Must be the first declaration in the file
• If included later in the file, it will generate a compile error
Development Advantages• Assists in testing by reducing code complexity
and unnecessary checks for coercive types
• “if (!is_numeric($var)) { … }”
• Provide failsafes for producing better quality code
• Prevent sub-types from breaking the expected return type of the super-type1), especially in interfaces
• Prevent unintended return values
• Document return type information in a way that is not easily invalidated (unlike comments)
Business Advantages• Higher quality code requires less
maintenance by reducing technical debt.
• Built in language features can speed up development cycles (or provide extended time for writing tests and improving overall QA) by simplifying common as well as complex tasks.
• Simplifying complex tasks provides a better platform for feature requests and issue reporting through project managers like JIRA, Asana, or Github Issues.
New Operators
• Null Coalesce [??]
• Checking for null
• typically done with isset()
• tedious and repetative
• Combined Comparison (“Spaceship”) [<=>]
• compares two values
• returns 0, 1, or -1
• primarily used for sorting
Null Coalesce
• Traditional snippet:
• if (isset($var)) { return $var;} else { return $b;}
• Ternary version:
• isset($var) ? $var : $b;
• New syntax:
• $var ?? $b;
Combined or “Three-way” Comparison / “Spaceship”
• Traditionally:
• usort($someList, function($first, $second) { if ($first == $second) { return 0; } return ($first > $second) ? 1 : -1;});
• New syntax:
• usort($someList, function($first, $second) { return $first <=> $second;});
Development Advantages• Assists by reducing code complexity
through more concise syntax
• Readability is significantly increased
• Some details
• Similar to strcmp() or version_compare()
• it has the same precedence as == and !=
Business Advantages• Higher quality code requires less
maintenance by reducing technical debt.
• Built in language features can speed up development cycles (or provide extended time for writing tests and improving overall QA) by simplifying common as well as complex tasks.
• Simplifying complex tasks provides a better platform for feature requests and issue reporting through project managers like JIRA, Asana, or Github Issues.
Performance Something everyone loves
Performance Benchmark Comparison
October 14th, 2014 : Andi Gutmans Co-founder of Zend, PHP Co-architect
Recent Benchmarks
Test Box Specs • Gigabyte Z87X-UD3H i7-4771
4 cores @ 3.50GHz w/ 16G of Ram @ 1600MHz
• Hyperthreading enabled for a total of 8 virtual cores
• Toshiba THNSNHH256GBST SSD
• Linux debian 3.16.0-4-amd64 #1SMP Debian 3.16.7-ckt9-2 (2015-04-13) x86_64 GNU/Linux
• MySQL 5.6.24
• nginx-1.6.2 + php-fpm for all testsunless indicated otherwise
• Quiet local 100Mbps network
• siege benchmark tool run from a separate machine
* Not performed on same test machine, but values are comparable
7.0 - dev 5.6
Drupal7.27* 490 r/s 310 r/s
Drupal8 - git 2580 r/s 1401 r/s
WordPress4.1 604 r/s 270 r/s
Development Advantages• Faster turnover for all tasks:
• Running test suites
• Running VMs
• Browser testing
• Faster is pretty much better, amirite?
Business Advantages• PHP 7 is lighter on memory and processing
which correlates directly to servers needed for running web applications.
• The more req/sec we can handle, the less need there is to scale earlier.
• In larger applications, it may be possible to push out the need to optimize. This opens up the opportunity to guide development toward providing more features for customers/consumers or handle squash more bugs.
Other Considerations• Many of the functions that have been flagged
as E_DEPRECATED from PHP 4.x have been deleted. See https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7for more info.
• New reserved words (mostly scalars, but some others)
• All old script tags and ASP style tags support has been removed
• Closure::call
• Group use declarations
• Engine Exceptions for catching recoverable fatal errors.
• Generator Return Expressions and Delegation
• Unicode Codepoint Escape Syntax (“\u{CODEPOINT}”)
• Consistency fixes:
• $obj->$property[‘name’];
• php < 7
• $obj->{$property[‘name’]}
• php 7 +
• {$obj->$property}[‘name’]
That’s all, PHPolks.
Questions?