Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP 7 Crash Course

773 views

Published on

PHP 7 was recently released, bringing some much-desired changes and improvements to the language. However, many developers haven't had the opportunity to use it for their projects and may not be familiar with the changes it brings. 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.

Published in: Internet
  • Be the first to comment

  • Be the first to like this

PHP 7 Crash Course

  1. 1. PHP 7 Crash Course Colin O’Dell / @colinodell
  2. 2. 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
  3. 3. Overview • New Features • Changes & Enhancements • Deprecations • Removed Features • Installing PHP 7
  4. 4. New Features
  5. 5. 1. Scalar Type Hints
  6. 6. 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
  7. 7. 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
  8. 8. 1. Scalar Type Hints: Strong / Strict
  9. 9. 1. Scalar Type Hints: Strong / Strict
  10. 10. 1. Scalar Type Hints: Strong / Strict
  11. 11. 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
  12. 12. 2. Return Type Declarations
  13. 13. 2. Return Type Declarations
  14. 14. 2. Return Type Declarations - Scalars
  15. 15. 2. Return Type Declarations - null 1. Matches behavior of parameter types 2. Guarantees you’ll never get a null value returned
  16. 16. 3. Combined Comparison Operator (aka T_SPACESHIP) 3. Spaceship Operator 3. Combined Comparison Operator (expr1) (expr2)
  17. 17. 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
  18. 18. 3. Spaceship Operator 3. Combined Comparison Operator
  19. 19. 3. Spaceship Operator 3. Combined Comparison Operator
  20. 20. 3. Sorting with T_SPACESHIP
  21. 21. 3. Sorting with T_SPACESHIP
  22. 22. Sorting by multiple values
  23. 23. Sorting by multiple values
  24. 24. 4. Null Coalesce Operator: ??
  25. 25. 5. Unicode Codepoint Escape Syntax ☃ (0x2603)
  26. 26. 6. Anonymous Classes
  27. 27. 6. Anonymous Classes
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. Changes & Improvements
  32. 32. 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
  33. 33. 2. Uniform Variable Syntax
  34. 34. 2. Uniform Variable Syntax – PHP 5.x
  35. 35. 2. Uniform Variable Syntax – PHP 5.x
  36. 36. 2. Uniform Variable Syntax – PHP 7.x
  37. 37. 2. Uniform Variable Syntax – PHP 7.x
  38. 38. 2. Uniform Variable Syntax – BC Breaks
  39. 39. 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
  40. 40. 3. Semi-Reserved Words
  41. 41. 3. Semi-Reserved Words
  42. 42. 3. Semi-Reserved Words
  43. 43. 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
  44. 44. 4. Error Handling & Exceptions • Fatal & recoverable fatal errors are now thrown like exceptions • You can catch them! • New Throwable interface:
  45. 45. 4. Error Handling & Exceptions
  46. 46. 5. Filtered unserialize()
  47. 47. 5. Filtered unserialize()
  48. 48. 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
  49. 49. Deprecations & Removals
  50. 50. 1. Deprecation of PHP 4 Constructors
  51. 51. 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.
  52. 52. 2. Deprecation of salt Option for password_hash Image source: preachersinstitute.com
  53. 53. 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
  54. 54. 4. Removal of Alternative PHP Tags <% // ... %> <%= // ... %> <script language="php"> // ... </script> <? //... ?> <?= //... ?> <?php //... ?>
  55. 55. 5. Reclassification of E_STRICT Notices
  56. 56. 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
  57. 57. 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
  58. 58. Installing PHP 7
  59. 59. 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
  60. 60. CentOS / RHEL CentOS/RHEL 7.x: rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm yum install php70w CentOS/RHEL 6.x: rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm yum install php70w
  61. 61. 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
  62. 62. phpbrew https://github.com/phpbrew/phpbrew phpbrew update phpbrew install php-7.0.4
  63. 63. 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 bit.ly/lsp-php7
  64. 64. Questions?
  65. 65. Slides & Feedback https://joind.in/17624 Thanks!! Colin O’Dell @colinodell

×