Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
PHP 7 Crash Course
Colin O’Dell
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
Overview
• New Features
• Changes & Enhancements
• Deprecations
• Removed Features
• Installing PHP 7
PHP 7 Crash Course - php[world] 2015
New Features
1. Scalar Type Hints
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
1. Scalar Type Hints
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
1. Scalar Type Hints: Strong / Strict
1. Scalar Type Hints: Strong / Strict
1. Scalar Type Hints: Strong / Strict
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
2. Return Type Declarations
2. Return Type Declarations
2. Return Type Declarations - Scalars
2. Return Type Declarations - null
1. Matches behavior of parameter types
2. Guarantees you’ll never get a null value returned
3. Combined Comparison Operator
(aka T_SPACESHIP)
3. Spaceship Operator
3. Combined Comparison Operator
(expr1) (expr2)
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
3. Spaceship Operator
3. Combined Comparison Operator
3. Spaceship Operator
3. Combined Comparison Operator
3. Sorting with T_SPACESHIP
3. Sorting with T_SPACESHIP
Sorting by multiple values
Sorting by multiple values
4. Null Coalesce Operator: ??
5. Unicode Codepoint Escape Syntax
☃ (0x2603)
6. Anonymous Classes
6. Anonymous Classes
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
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
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
Changes &
Improvements
1. Performance
0
100
200
300
400
500
600
Drupal 7 WordPress 4.1 Laravel ZF 2 SugarCRM
Requests Per Minute
PHP 5.6 HHVM PHP 7.0
Source: http://www.zend.com/en/resources/php7_infographic
2. Uniform Variable Syntax
2. Uniform Variable Syntax – PHP 5.x
2. Uniform Variable Syntax – PHP 5.x
2. Uniform Variable Syntax – PHP 7.x
2. Uniform Variable Syntax – PHP 7.x
2. Uniform Variable Syntax – BC Breaks
3. Semi-Reserved Words
abstract
and
array
as
break
callable
case
catch
class*
clone
const
continue
declare
default
die
do
echo
else
elseif
enddeclare
endfor
endforeach
endif
endswitch
endwhile
exit
extends
final
finally
for
foreach
function
global
goto
if
implements
include
include_once
instanceof
insteadof
interface
list
namespace
new
or
parent
print
private
protected
public
require
require_once
return
self
static
switch
throw
trait
try
use
var
while
xor
yield
3. Semi-Reserved Words
3. Semi-Reserved Words
3. Semi-Reserved Words
3. Semi-Reserved Words
abstract
and
array
as
break
callable
case
catch
class*
clone
const
continue
declare
default
die
do
echo
else
elseif
enddeclare
endfor
endforeach
endif
endswitch
endwhile
exit
extends
final
finally
for
foreach
function
global
goto
if
implements
include
include_once
instanceof
insteadof
interface
list
namespace
new
or
parent
print
private
protected
public
require
require_once
return
self
static
switch
throw
trait
try
use
var
while
xor
yield
4. Error Handling & Exceptions
• Fatal & recoverable fatal errors are now thrown like exceptions
• You can catch them!
• New Throwable interface:
4. Error Handling & Exceptions
5. Filtered unserialize()
5. Filtered unserialize()
Changes & Improvements: Summary
We Covered:
1. Performance
2. Uniform Variable Syntax
3. Semi-Reserved Words
4. Error Handling & Exceptions
5. 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
Deprecations &
Removals
1. Deprecation of PHP 4 Constructors
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.
2. Deprecation of salt Option for
password_hash
Image source: preachersinstitute.com
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
4. Removal of Alternative PHP Tags
<% // ... %>
<%= // ... %>
<script language="php"> // ... </script>
<? //... ?>
<?= //... ?>
<?php //... ?>
5. Reclassification of E_STRICT Notices
5. Reclassification of E_STRICT Notices
E_ERROR
E_WARNING
E_PARSE
E_NOTICE
E_CORE_ERROR
E_COMPILE_ERROR
E_COMPILE_WARNING
E_USER_ERROR
E_USER_WARNING
E_STRICT
E_RECOVERABLE_ERROR
E_DEPRECATED
E_USER_DEPRECATED
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
Installing PHP 7
November
Ubuntu Apt Packages
Ondřej Surý
https://launchpad.net/~ondrej/+archive/ubuntu/php-7.0
sudo apt-get remove php5*
sudo add-apt-repository ppa:ondrej/php-7.0
sudo apt-get update
sudo apt-get install php7.0
sudo a2enmod php7.0
sudo service apache2 restart
Zend Nightlies
http://php7.zend.com/
Binary packages for:
CentOS 7
Ubuntu 14.04
Debian 8
Dockerfile
Tarballs
Compiling From Source
Ubuntu/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
phpbrew
https://github.com/phpbrew/phpbrew
phpbrew update
phpbrew install next as php-7.0.0
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
Questions?
PHP 7 Crash Course - php[world] 2015
Feedback
https://joind.in/14751
@colinodell
Thanks!!
PHP 7 Crash Course - php[world] 2015
PHP 7 Crash Course - php[world] 2015

More Related Content

PHP 7 Crash Course - php[world] 2015

Editor's Notes

  1. 13 years JavaScript, C#, and Java
  2. !!!This talk won’t be exhaustive Most-common things you’ll encounter Get you up-and-running
  3. SCAY-LER PHP 5 – classes, array, and callable PHP 7 – string, bool, int, & float
  4. SCAY-LER
  5. Directive must be on first line; compiler error Because applied per-file: can mix-and-match modes
  6. Directive must be on first line; compiler error Because applied per-file: can mix-and-match modes
  7. Directive must be on first line; compiler error Because applied per-file: can mix-and-match modes
  8. NO FIX SLIDE
  9. CSPRNG NEXT
  10. 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.
  11. phpng Using more-compact data structures • Reducing the number of heap allocations and deallocations • Utilizing native thread local storage
  12. Variable-variable syntax
  13. Variable-variable syntax
  14. Variable-variable syntax
  15. Variable-variable syntax
  16. Context-sensitive lexer Properties, constants and methods classes, interfaces and traits
  17. Properties, constants and methods classes, interfaces and traits
  18. 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.
  19. PHP 5.5
  20. MENTION STABLE RELEASE DATE
  21. 8 days
  22. END OF TALK!