The why and how of moving to php 5.4/5.5

Embed Size (px)

DESCRIPTION

With PHP 5.5 out and many production environments still running 5.2 (or older), it's time to paint a clear picture on why everyone should move to 5.4 and 5.5 and how to get code ready for the latest version of PHP. In this talk, we'll look at some handy tools and techniques to ease the migration.

Citation preview

  • 1. The Why and How of moving to PHP 5.4/5.5

2. Questions during the talk ? Ask at http://wpc13.cnf.io 3. Who am I ? Wim Godden (@wimgtr) Founder of Cu.be Solutions (http://cu.be) Open Source developer since 1997 OpenX, PHPCompatibility, Nginx SCL, ... Speaker at PHP and Open Source conferences 4. Why vs How Part 1 : why upgrade ? Bad reasons : It's cool to have the latest version Annoy sysadmins Oh cool, a new toy ! Part 2 : how to upgrade ? The nightmare of compatibility The joy of automation No miracles here ! 5. Show of hands 3 / 4 5.0 5.1 5.2 5.3 5.4 5.5 6.0 (just kidding) 6. The numbers W3Techs (http://w3techs.com/technologies/details/pl-php/all/all) Now May 2013 PHP 4 : 2.9% 2.7% PHP 5 : 97.1% 97.3% 5.0 : 0.1% 0.1% 5.1 : 2.2% 2.6% 5.2 : 40.2% 43.5% 5.3 : 51.7% 49.7% 5.4 : 5.7% 4.1% 5.5 : 0.1 % < 0.1% 5.6 : < 0.1% 7. 5.3 quick recap Namespaces () Late static binding Closures Better garbage collection Goto Mysqlnd Performance gain 8. 5.3 people are not even using it ! 40.2% still on PHP 5.2 No : Symfony 2 Zend Framework 2 Other frameworks that need namespaces Problematic for developers 9. PHP 5.4/5.5 what's changed ? New features Performance and memory usage Improved consistency Some things removed 10. New things short array syntax (5.4) $yourItems = array('a', 'b', 'c', 'd'); $yourItems = ['a', 'b', 'c', 'd']; $yourItems = ['a' => 5, 'b' => 3]; 11. New things function array dereferencing (5.4) function getCars() { return array( 'Mini', 'Smart', 'Volvo', 'BMW' ); } $cars = getCars(); echo $cars[1]; function getCars() { return [ 'Mini', 'Smart', 'Volvo', 'BMW' ]; } echo getCars()[1]; 12. New things Traits Reuse methods across classes Classes have no common parent 13. Traits - example Log output : abcd trait Logger { public function log($data) { echo "Log output : " . $data; } } class SomeClass { use Logger; } $someObject = new SomeClass(); $someObject->log('abcd'); 14. Traits - example class SomeClass { public function log($data) { echo "Log output : " . $data; } } $someObject = new SomeClass(); $someObject->log('abcd'); 15. Traits careful ! trait Logger { private $foo; } class SomeClass { private $foo; use Logger; } will throw E_STRICT ! 16. New things Webserver (5.4) Built-in webserver Development only ! Handles requests sequentially Ideal for quick testing Ideal for unit testing of webservices 17. Webserver how to /var/www/php54test/html> php -S localhost:8000 PHP 5.4.15 Development Server started at Sat May 2 00:41:12 2013 Listening on http://localhost:8000 Document root is /var/www/php54test/html Press Ctrl-C to quit. /var/www/php54test/html> php -S localhost:8000 -t /var/www/other-path/html PHP 5.4.15 Development Server started at Sat May 2 00:41:12 2013 Listening on http://localhost:8000 Document root is /var/www/other-path/html Press Ctrl-C to quit. /var/www/php54test/html> php -S localhost:8000 bootstrap.php PHP 5.4.15 Development Server started at Sat May 2 00:41:12 2013 Listening on http://localhost:8000 Document root is /var/www/php54test/html Press Ctrl-C to quit. 18. New things SessionHandler (5.4) New session handling class Groups all methods for session handling : close() destroy() gc() open() read() write() 19. SessionHandler class MySessionHandler extends SessionHandler { public function read($session_id) { // Get the session data } public function write($session_id, $session_data) { // Set the session data } ... } $handler = new MySessionHandler(); session_set_save_handler($handler, true); session_start(); 20. New things more session stuff (5.4) File upload extension file upload registers in session readable through AJAX calls New function session_status() Return values : PHP_SESSION_ACTIVE / PHP_SESSION_NONE 21. New things Generators (5.5) Simple way of implementing iterators Simply put : foreach over a function Say what ? 22. Generators - example