php 7 crash course
TRANSCRIPT
![Page 1: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/1.jpg)
PHP 7 Crash CourseColin O’Dell / @colinodell
![Page 2: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/2.jpg)
Colin O’Dell
Lead Web Developer at Unleashed Technologies PHP developer since 2002 PHP League Member league/commonmark league/html-to-markdown PHP 7 Upgrade Guide e-book
@colinodell / www.colinodell.com
![Page 3: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/3.jpg)
Overview• New Features• Changes & Enhancements• Deprecations• Removed Features• Installing PHP 7
![Page 4: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/4.jpg)
![Page 5: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/5.jpg)
New Features
![Page 6: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/6.jpg)
1. Scalar Type Hints
![Page 7: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/7.jpg)
1. Scalar Type HintsTwo type checking modes:
Weak (“coercive”) Different types are
“coerced” (cast) to the desired type
Default mode
Strong (“strict”) Parameter types must*
be identical
Must be enabled per-file
![Page 8: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/8.jpg)
![Page 9: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/9.jpg)
1. Scalar Type Hints: Weak / Coercive
Type Declaration
int float string bool object
int yes yes* yes† yes nofloat yes yes yes† yes nostring yes yes yes yes yes‡bool yes yes yes yes no
* Only non-NaN floats between PHP_INT_MIN and PHP_INT_MAX accepted.† If it’s a numeric string‡ Only if object has a __toString() method
![Page 10: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/10.jpg)
1. Scalar Type Hints: Strong / Strict
![Page 11: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/11.jpg)
1. Scalar Type Hints: Strong / Strict
![Page 12: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/12.jpg)
1. Scalar Type Hints: Strong / Strict
![Page 13: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/13.jpg)
1. Scalar Type Hints: Strong / Strict
Type Declaration
int float string bool object
int yes no no no nofloat yes* yes no no nostring no no yes no nobool no no no yes no
* Allowed due to widening primitive conversion
![Page 14: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/14.jpg)
2. Return Type Declarations
![Page 15: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/15.jpg)
2. Return Type Declarations
![Page 16: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/16.jpg)
2. Return Type Declarations - Scalars
![Page 17: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/17.jpg)
2. Return Type Declarations - null
1.Matches behavior of parameter types2.Guarantees you’ll never get a null value
returned
![Page 18: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/18.jpg)
<=>
3. Combined Comparison Operator
(aka T_SPACESHIP)
3. Spaceship Operator3. Combined Comparison Operator
(expr1)
(expr2)
![Page 19: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/19.jpg)
3. Spaceship Operator3. Combined Comparison Operator
(expr1) <=> (expr2)Returns:
0 If both expressions are equal
1 If the left is greater-1 If the right is
greater
![Page 20: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/20.jpg)
3. Spaceship Operator3. Combined Comparison Operator
![Page 21: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/21.jpg)
3. Spaceship Operator3. Combined Comparison Operator
![Page 22: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/22.jpg)
3. Sorting with T_SPACESHIP
![Page 23: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/23.jpg)
3. Sorting with T_SPACESHIP
![Page 24: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/24.jpg)
Sorting by multiple values
![Page 25: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/25.jpg)
Sorting by multiple values
![Page 26: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/26.jpg)
4. Null Coalesce Operator: ??
![Page 27: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/27.jpg)
5. Unicode Codepoint Escape Syntax ☃ (0x2603)
![Page 28: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/28.jpg)
6. Anonymous Classes
![Page 29: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/29.jpg)
6. Anonymous Classes
![Page 30: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/30.jpg)
6. Anonymous Classes
Use Cases:
• Creating simple, single-use classes• Quickly implementing a light-weight interface (like a logger or event observer)
• Overriding a single field/method of a class without having to subclass it
• Mocking tests by creating implementations on-the-fly
![Page 31: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/31.jpg)
7. User-Land CSPRNG API
Alternatives:Not cryptographically-secure:
rand()mt_rand()
Requires an extension:openssl_random_pseudo_bytes()mcrypt_create_iv()
Support varies per platform:/dev/arandom/dev/urandom
![Page 32: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/32.jpg)
New Features: SummaryWe Covered:1. Scalar Type Hints2. Return Type Declarations3. Combined Comparison
Operator4. Null Coalesce Operator5. Unicode Codepoint Escape
Syntax6. Anonymous Classes7. User-Land CSPRNG API
Other Areas to Explore: Group Use Syntax Closure Call Method Generator Return Expressions Generator Delegation Integer Division Function preg_replace_callback_array IntlChar class
![Page 33: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/33.jpg)
Changes & Improvements
![Page 34: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/34.jpg)
1. Performance
Drupal 7 WordPress 4.1
Laravel ZF 2 SugarCRM0
100
200
300
400
500
600
Requests Per MinutePHP 5.6 HHVM PHP 7.0
Source: http://www.zend.com/en/resources/php7_infographic
![Page 35: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/35.jpg)
2. Uniform Variable Syntax
![Page 36: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/36.jpg)
2. Uniform Variable Syntax – PHP 5.x
![Page 37: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/37.jpg)
2. Uniform Variable Syntax – PHP 5.x
![Page 38: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/38.jpg)
2. Uniform Variable Syntax – PHP 7.x
![Page 39: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/39.jpg)
2. Uniform Variable Syntax – PHP 7.x
![Page 40: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/40.jpg)
2. Uniform Variable Syntax – BC Breaks
![Page 41: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/41.jpg)
3. Semi-Reserved Wordsabstractandarrayasbreakcallablecasecatchclass*cloneconstcontinuedeclaredefault
diedoechoelseelseifenddeclareendforendforeachendifendswitchendwhileexitextendsfinal
finallyforforeachfunctionglobalgotoifimplementsincludeinclude_onceinstanceofinsteadof
interfacelistnamespaceneworparentprintprivateprotectedpublicrequirerequire_oncereturn
selfstaticswitchthrowtraittryusevarwhilexoryield
![Page 42: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/42.jpg)
3. Semi-Reserved Words
![Page 43: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/43.jpg)
3. Semi-Reserved Words
![Page 44: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/44.jpg)
3. Semi-Reserved Words
![Page 45: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/45.jpg)
3. Semi-Reserved Wordsabstractandarrayasbreakcallablecasecatchclass*cloneconstcontinuedeclaredefault
diedoechoelseelseifenddeclareendforendforeachendifendswitchendwhileexitextendsfinal
finallyforforeachfunctionglobalgotoifimplementsincludeinclude_onceinstanceofinsteadof
interfacelistnamespaceneworparentprintprivateprotectedpublicrequirerequire_oncereturn
selfstaticswitchthrowtraittryusevarwhilexoryield
![Page 46: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/46.jpg)
4. Error Handling & Exceptions• Fatal & recoverable fatal errors are now thrown like exceptions• You can catch them!• New Throwable interface:
![Page 47: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/47.jpg)
4. Error Handling & Exceptions
![Page 48: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/48.jpg)
5. Filtered unserialize()
![Page 49: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/49.jpg)
5. Filtered unserialize()
![Page 50: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/50.jpg)
Changes & Improvements: SummaryWe Covered:1. Performance2. Uniform Variable Syntax3. Semi-Reserved Words4. Error Handling & Exceptions5. Filtered unserialize()
Other Areas to Explore: Abstract Syntax Tree Division By Zero Semantics Expectations Array Constants With define() session_start() options Reflection Enhancements JSON Library Behavior Changes to foreach Behavior Changes to list Parameter Handling Changes Custom Session Handler Return Values Errors on Invalid Octal Literals
![Page 51: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/51.jpg)
Deprecations & Removals
![Page 52: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/52.jpg)
1. Deprecation of PHP 4 Constructors
![Page 53: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/53.jpg)
1. Deprecation of PHP 4 Constructors
1.E_STRICT is no longer emitted when both types are present.
2.E_DEPRECATED emitted whenever any PHP 4-style constructor is used.
![Page 54: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/54.jpg)
2. Deprecation of salt Option for password_hash
Image source: preachersinstitute.com
![Page 55: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/55.jpg)
3. Removal of Previously-Deprecated Features23 deprecated features have been completely removed!
Three examples: ext/mysql extension
# Old-style comments in php.ini; (Use new-style comments instead)
Advice: check for deprecation warnings in 5.6
![Page 56: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/56.jpg)
4. Removal of Alternative PHP Tags<% // ... %><%= // ... %><script language="php"> // ... </script>
<? //... ?><?= //... ?><?php //... ?>
![Page 57: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/57.jpg)
5. Reclassification of E_STRICT Notices
![Page 58: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/58.jpg)
5. Reclassification of E_STRICT Notices
E_ERRORE_WARNINGE_PARSEE_NOTICEE_CORE_ERRORE_COMPILE_ERRORE_COMPILE_WARNINGE_USER_ERRORE_USER_WARNINGE_STRICTE_RECOVERABLE_ERRORE_DEPRECATEDE_USER_DEPRECATED
![Page 59: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/59.jpg)
Deprecations & Removals: SummaryWe Covered:1. Deprecation of PHP 4
Constructors2. Deprecation of salt Option for
password_hash3. Removal of Previously-
Deprecated Features4. Removals of Alternative PHP
Tags5. Reclassification of E_STRICT
Notices
Other Areas to Explore: Removal of Multiple Defaults in Switches
Removal of Numeric Hexadecimal String Support
Removal of Dead SAPIs and Extensions
Removal of the date.timezone Warning
![Page 60: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/60.jpg)
Installing PHP 7
![Page 61: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/61.jpg)
Ubuntu Apt PackagesOndřej Surýhttps://launchpad.net/~ondrej/+archive/ubuntu/php-7.0
sudo apt-get remove php5*
sudo add-apt-repository ppa:ondrej/php-7.0sudo apt-get updatesudo apt-get install php7.0
sudo a2enmod php7.0sudo service apache2 restart
![Page 62: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/62.jpg)
CentOS / RHELCentOS/RHEL 7.x:rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpmrpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpmyum install php70w
CentOS/RHEL 6.x:rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpmyum install php70w
![Page 63: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/63.jpg)
Compiling From SourceUbuntu/Debian:• http://www.zimuel.it/install-php-7/• http://
www.hashbangcode.com/blog/compiling-and-installing-php7-ubuntu• https://www.howtoforge.com/tutorial/install-php-7-on-debian-8-jessie
/
OS X:• https://gist.github.com/denji/8e50fcb13482c5d6c78a
Windows:• http://www.kshabazz.net/build-php-on-windows.html
![Page 64: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/64.jpg)
phpbrew https://github.com/phpbrew/phpbrew
phpbrew updatephpbrew install php-7.0.4
![Page 65: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/65.jpg)
Additional ResourcesOfficial PHP Resources PHP Manual: Migrating from PHP 5.6.x to PHP 7.0.x PHP 7 UPGRADING doc PHP 7 RFCs
Community Resources (free & paid) Getting Ready for PHP 7 What to Expect When You’re Expecting: PHP 7, Part 1 What to Expect When You’re Expecting: PHP 7, Part 2 Zend: 5 Things You Must Know About PHP 7 The PHP 7 Revolution: Return Types and Removed Artifacts PHP 7: 10 Things You Need to Know #php7 on Twitter tpunt/PHP7-Reference GoPHP7 Extensions Project Laracasts – PHP 7 Up and Running PHP 7 Upgrade Guide
bit.ly/lsp-php7
![Page 66: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/66.jpg)
Questions?
![Page 67: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/67.jpg)
Slides & Feedback https://joind.in/17624
Thanks!!
Colin O’Dell@colinodell
![Page 68: PHP 7 Crash Course](https://reader036.vdocuments.site/reader036/viewer/2022062306/587ed1581a28abdb198b5281/html5/thumbnails/68.jpg)