The PHP 7 release is just around the corner, bringing some much-desired changes and improvements to the language. However, many developers simply aren't aware of what's coming. We'll remedy this by checking out the new "spaceship operator," demonstrating how static type hints produce clean code, and using anonymous classes to quickly implement interfaces on the fly. Attendees will also learn about breaking changes and "gotchas" to watch out for when making the upgrade and will receive pointers on getting started with PHP 7 today.
2. Colin O’Dell
Lead Web Developer at Unleashed Technologies
PHP developer since 2002
Certifications: Symfony and Magento
PHP League Member
league/commonmark
league/html-to-markdown
PHP 7 Migration Guide e-book
@colinodell / www.colinodell.com
3. Overview
• New Features
• Changes & Enhancements
• Deprecations
• Removed Features
• Installing PHP 7
7. 1. Scalar Type Hints
Two 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
9. 1. Scalar Type Hints: Weak / Coercive
Type Declaration int float string bool object
int yes yes* yes† yes no
float yes yes yes† yes no
string 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
13. 1. Scalar Type Hints: Strong / Strict
Type Declaration int float string bool object
int yes no no no no
float yes* yes no no no
string no no yes no no
bool no no no yes no
* Allowed due to widening primitive conversion
19. 3. Spaceship Operator
3. Combined Comparison Operator
(expr1) <=> (expr2)
Returns:
0 If both expressions are equal
1 If the left is greater
-1 If the right is greater
30. 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
31. 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
32. New Features: Summary
We Covered:
1. Scalar Type Hints
2. Return Type Declarations
3. Combined Comparison Operator
4. Null Coalesce Operator
5. Unicode Codepoint Escape Syntax
6. Anonymous Classes
7. 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
53. 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.
54. 2. Deprecation of salt Option for
password_hash
Image source: preachersinstitute.com
55. 3. Removal of Previously-Deprecated Features
23 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
59. Deprecations & Removals: Summary
We Covered:
1. Deprecation of PHP 4 Constructors
2. Deprecation of salt Option for
password_hash
3. Removal of Previously-Deprecated
Features
4. Removals of Alternative PHP Tags
5. 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
66. Additional Resources
Official 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
php7book.com/phpworld
Directive must be on first line; compiler error
Because applied per-file: can mix-and-match modes
Directive must be on first line; compiler error
Because applied per-file: can mix-and-match modes
Directive must be on first line; compiler error
Because applied per-file: can mix-and-match modes
NO FIX SLIDE
CSPRNG NEXT
LAST NEW
cryptographically-secure psueod-random number generator
vastly simplifies the process of generating high-quality random data
On Windows, » CryptGenRandom() will always be used.
On Linux, the » getrandom(2) syscall will be used if available.
On other platforms, /dev/urandom will be used.
If none of the aforementioned sources are available, then an Exception will be thrown.
phpng
Using more-compact data structures • Reducing the number of heap allocations and deallocations • Utilizing native thread local storage
Variable-variable syntax
Variable-variable syntax
Variable-variable syntax
Variable-variable syntax
Context-sensitive lexer
Properties, constants and methods
classes, interfaces and traits
Properties, constants and methods
classes, interfaces and traits
If a class is not allowed, PHP will unserialize it as an “incomplete class” object (__PHP_Incomplete_- Class). This is the same behavior PHP already uses when you try to unserialize a class which doesn’t exist.