Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's New in PHP 5.5

Avatar for Corey Ballou Corey Ballou
September 12, 2013

What's New in PHP 5.5

Follow me on Twitter at http://www.twitter.com/cballou or checkout my startup at http://www.pop.co.

In this presentation we will cover the best features and additions in PHP 5.5. You can look forward to the following:

* Support for generators has been added via the yield keyword
* Usage of the new finally keyword in try-catch blocks
* An overview and examples of the new password hashing API
* The foreach control structure now supports unpacking nested arrays into separate variables via the list() construct
* empty() supports arbitrary expressions such as closures returning false
* array and string literal dereferencing
* The Zend Optimiser+ opcode cache (via the new OPcache extension)

Avatar for Corey Ballou

Corey Ballou

September 12, 2013
Tweet

More Decks by Corey Ballou

Other Decks in Technology

Transcript

  1. GENERATORS Support for generators has been added via the yield

    keyword r a n g e ( 0 , 1 0 0 0 0 0 0 ) vs. user supplied function
  2. EXAMPLE USAGE OF YIELD KEYWORD f u n c t

    i o n g e t L i n e s ( $ f i l e p a t h ) { $ f = f o p e n ( $ f i l e p a t h , ' r ' ) ; t r y { w h i l e ( $ l i n e = f g e t s ( $ f ) ) { y i e l d $ l i n e ; } } f i n a l l y { f c l o s e ( $ f ) ; } } f o r e a c h ( g e t L i n e s ( " f i l e . t x t " ) a s $ n = > $ l i n e ) { e c h o $ l i n e ; }
  3. THE "FINALLY" KEYWORD IS FINALLY HERE Execute code ALWAYS. t

    r y { t h r o w n e w E x c e p t i o n ( ' h e l l o ' ) ; } c a t c h ( E x c e p t i o n $ e ) { e c h o $ e - > g e t M e s s a g e ( ) ; } f i n a l l y { / / t h i s c o d e w i l l a l w a y s b e r u n e c h o ' , w o r l d ' ; }
  4. PASSWORD HASHING API A super easy library that uses underlying

    crypt library. p a s s w o r d _ g e t _ i n f o ( ) p a s s w o r d _ h a s h ( ) p a s s w o r d _ n e e d s _ r e h a s h ( ) p a s s w o r d _ v e r i f y ( )
  5. EXAMPLE OF REGISTER/LOGIN f u n c t i o

    n r e g i s t e r ( $ u s e r n a m e , $ p a s s w o r d ) { $ h a s h = p a s s w o r d _ h a s h ( $ p a s s w o r d , P A S S W O R D _ B C R Y P T ) ; / / s a v e h a s h t o d a t a b a s e } f u n c t i o n l o g i n ( $ u s e r n a m e , $ p a s s w o r d ) { $ h a s h = g e t H a s h F r o m D b B y U s e r n a m e ( $ u s e r n a m e ) ; i f ( p a s s w o r d _ v e r i f y ( $ p a s s w o r d , $ h a s h ) ) { / / p e r f o r m l o g i n ( s t o r e s e s s i o n v a r ) r e t u r n t r u e ; } r e t u r n f a l s e ; }
  6. INCREASING PASSWORD SECURITY You can optionally supply your own salt

    and algorithmic cost. f u n c t i o n r e g i s t e r ( $ u s e r n a m e , $ p a s s w o r d ) { $ o p t i o n s = a r r a y ( ' s a l t ' = > ' s o m e R a n d o m S a l t ' , ' c o s t ' = > 1 2 ) ; $ h a s h = p a s s w o r d _ h a s h ( $ p a s s w o r d , P A S S W O R D _ B C R Y P T , $ o p t i o n s ) ; / / s a v e h a s h t o d a t a b a s e }
  7. EXAMPLE OF UPGRADING YOUR HASHING ALGORITHM f u n c

    t i o n l o g i n ( $ u s e r n a m e , $ p a s s w o r d ) { $ h a s h = g e t H a s h F r o m D b B y U s e r n a m e ( $ u s e r n a m e ) ; i f ( p a s s w o r d _ v e r i f y ( $ p a s s w o r d , $ h a s h ) ) { / / c h e c k i f h a s h i s i n u p d a t e d f o r m a t i f ( p a s s w o r d _ n e e d s _ r e h a s h ( $ h a s h , P A S S W O R D _ B C R Y P T ) ) { / / p e r f o r m u p d a t e $ h a s h = p a s s w o r d _ h a s h ( $ p a s s w o r d , P A S S W O R D _ B C R Y P T ) ; / / s a v e n e w h a s h t o d a t a b a s e } / / p e r f o r m l o g i n ( s t o r e s e s s i o n v a r ) r e t u r n t r u e ; } r e t u r n f a l s e ; }
  8. FOREACH + LIST $ a r r a y =

    [ [ 1 , 2 ] , [ 3 , 4 ] , ] ; f o r e a c h ( $ a r r a y a s l i s t ( $ a , $ b ) ) { e c h o " A : $ a ; B : $ b \ n " ; }
  9. A R R A Y _ C O L U

    M N ( ) FUN $ p e o p l e = [ [ ' f i r s t n a m e ' = > ' J o h n ' , ' l a s t n a m e ' = > ' D o e ' ] , [ ' f i r s t n a m e ' = > ' J a n e ' , ' l a s t n a m e ' = > ' D o e ' ] , ] ; / / c o n t a i n s [ 0 = > ' J o h n ' , 1 = > ' J a n e ' ] $ f i r s t n a m e s = a r r a y _ c o l u m n ( $ p e o p l e , ' f i r s t n a m e ' ) ;
  10. IMPROVEMENTS TO EMPTY() f u n c t i o

    n a l w a y s _ f a l s e ( ) { r e t u r n f a l s e ; } i f ( e m p t y ( a l w a y s _ f a l s e ( ) ) ) { e c h o " H e l l o , w o r l d . " ; }
  11. ARRAY AND STRING LITERAL DEREFERENCING / / a r r

    a y d e r e f e r e n c i n g e c h o [ 1 , 2 , 3 ] [ 0 ] ; / / s t r i n g d e r e f e r e n c i n g e c h o ' P H P ' [ 0 ] ;
  12. NOW LET'S REALLY GET CRAZY... f u n c t

    i o n f o o ( ) { r e t u r n a r r a y ( 1 , 2 , 3 ) ; } e c h o f o o ( ) [ 2 ] ; / / p r i n t s 3 $ f u n c = f u n c t i o n ( ) { r e t u r n a r r a y ( ' a ' , ' b ' , ' c ' ) ; } ; e c h o $ f u n c ( ) [ 0 ] ; / / p r i n t s a
  13. ZEND OPTIMISER+ OPCACHE EXTENSION Not a replacement for APC/memcache(d). No

    user cache! Available in PHP 5.4 via install. Source available: https://github.com/zend- dev/ZendOptimizerPlus $ p h p - v P H P 5 . 4 . 1 7 R C 1 ( c l i ) ( b u i l t : J u n 2 2 2 0 1 3 1 9 : 2 7 : 2 6 ) C o p y r i g h t ( c ) 1 9 9 7 - 2 0 1 3 T h e P H P G r o u p Z e n d E n g i n e v 2 . 4 . 0 , C o p y r i g h t ( c ) 1 9 9 8 - 2 0 1 3 Z e n d T e c h n o l o g i e s w i t h Z e n d O P c a c h e v 7 . 0 . 2 , C o p y r i g h t ( c ) 1 9 9 9 - 2 0 1 3 , b y Z e n d T e c h n o l o g i e s
  14. SO, UH, WHAT IS AN OPCODE CACHE? Component designed to

    speed up performance of PHP without altering the app. Overrides PHP's default compiler callback by checking if a compiled intermediate-code version of the code is available in-memory. It skips compilation when it can!
  15. WHAT TO DO ABOUT DEPRECATED APC MODULE? APC User Cache

    is in the works: APC minus the opcode cache! github.com/krakjoe/apcu
  16. BACKWARDS INCOMPATIBLE CHANGES Win XP/2003 support dropped self, parent and

    static are case insensitive p a c k ( ) and u n p a c k ( ) made more compatible with perl http://www.php.net/manual/en/migration55.deprecated.php
  17. CREDITS php.net - What has changed in PHP 5.5.x wiki.php.net

    - Integrating Zend Optimizer+ into the PHP distribution schlueters.de - Features in PHP trunk: Array dereferencing slideshare.net - What's new in PHP 5.5 stackoverflow.com - How do you use bcrypt for hashing passwords in PHP?