The why and how of moving to php 5.4

  • 1,801 views
Uploaded on

With PHP 5.4 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.3 and 5.4 and how to get code ready for the latest …

With PHP 5.4 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.3 and 5.4 and how to get code ready for the latest version of PHP. In this talk, we'll migrate an old piece of code using some standard and some very non-standard tools and techniques.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,801
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
10
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • part 2 look at difficulties you might encounter in upgrading. I'll provide solutions not a magician can't solve everything ;-)
  • 5.3.3 = Debian Squeezy = 12% Wait a second... that means people aren't even on 5.3 ? And there was 3 year gap between the release of 5.2 and 5.3, so 5.3 brought a lot of cool things.

Transcript

  • 1. The Why and How of moving to PHP 5.4
  • 2. Who am I ?Wim Godden (@wimgtr)Founder of Cu.be Solutions (http://cu.be)Open Source developer since 1997Developer of OpenX, PHPCompatibility, Nginx extensions, ...Zend Certified EngineerZend Framework Certified EngineerMySQL Certified DeveloperSpeaker at PHP and Open Source conferences
  • 3. Why vs HowPart 1 : why upgrade ?Bad reasons :Its cool to have the latest versionAnnoy sysadminsOh cool, a new toy !Part 2 : how to upgrade ?The nightmare of compatibilityThe joy of automationNo miracles here !
  • 4. Show of hands3 / 45.05.15.25.35.45.5RC16.0 (just kidding)
  • 5. The numbersW3Techs (http://w3techs.com/technologies/details/pl-php/all/all)PHP 4 : 2.7%PHP 5 : 97.2%5.0 : 0.1%5.1 : 2.6%5.2 : 43.5%5.3 : 49.7%5.4 : 4.1%5.5 : < 0.1 %
  • 6. 5.3 quick recapNamespaces ()Late static bindingClosuresBetter garbage collectionGotoMysqlndPerformance gain
  • 7. 5.3 – people are not even using it !43.5% still on PHP 5.2No :Symfony 2Zend Framework 2Other frameworks that need namespacesProblematic for developers
  • 8. PHP 5.4 – whats changed ?New featuresPerformance and memory usageImproved consistencySome things removed
  • 9. Exciting new things – short array syntax$yourItems = array(a, b, c, d);$yourItems = [a, b, c, d];$yourItems = [a => 5, b => 3];
  • 10. Exciting new things – function array dereferencingfunction getCars(){return array(Mini,Smart,Volvo,BMW);}$cars = getCars();echo $cars[1];function getCars(){return [Mini,Smart,Volvo,BMW];}echo getCars()[1];
  • 11. Exciting new things – TraitsReuse methods across classesClasses have no common parent
  • 12. Traits - exampleLog output : abcdtrait Logger{public function log($data) { echo "Log output : " . $data; }}class SomeClass {use Logger;}$someObject = new SomeClass();$someObject->log(abcd);
  • 13. Traits - exampleclass SomeClass {public function log($data) { echo "Log output : " . $data; }}$someObject = new SomeClass();$someObject->log(abcd);
  • 14. Traits – careful !trait Logger {private $foo;}class SomeClass {private $foo;use Logger;}will throw E_STRICT !
  • 15. Exciting new things – WebserverPHP 5.4 has a built-in webserverDevelopment only !Handles requests sequentiallyIdeal for quick testingIdeal for unit testing of webservices
  • 16. Webserver – how to/var/www/php54test/html> php -S localhost:8000PHP 5.4.15 Development Server started at Sat May 2 00:41:12 2013Listening on http://localhost:8000Document root is /var/www/php54test/htmlPress Ctrl-C to quit./var/www/php54test/html> php -S localhost:8000 -t /var/www/other-path/htmlPHP 5.4.15 Development Server started at Sat May 2 00:41:12 2013Listening on http://localhost:8000Document root is /var/www/other-path/htmlPress Ctrl-C to quit./var/www/php54test/html> php -S localhost:8000 bootstrap.phpPHP 5.4.15 Development Server started at Sat May 2 00:41:12 2013Listening on http://localhost:8000Document root is /var/www/php54test/htmlPress Ctrl-C to quit.
  • 17. Exciting new things – SessionHandlerNew session handling classGroups all methods for session handling :close()destroy()gc()open()read()write()
  • 18. SessionHandlerclass 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();
  • 19. Exciting new things – more session stuffFile upload extension→ file upload registers in session→ readable through AJAX callsNew function session_status()Return values : PHP_SESSION_ACTIVE / PHP_SESSION_NONE
  • 20. Performance and memory usagePerformance : 10 – 30%How ?Core optimizationsNew internal caches (functions, constants, …)Better (un)serializationInlining often-used code paths…Reduced memory usage : up to 50% !Big impact on large frameworksEven bigger impact on codebases such as Drupal
  • 21. What else is new ? (1/3)Binary notationDecimal : 123Octal : 0173Hex : 0x7BBinary : 0b1111011Class member access on object instantiation`$fooObj = new Foo();echo $fooObj->bar();echo (new Foo)->bar();
  • 22. What else is new ? (2/3)class Cat {private $meows;function __construct($meows) {$this->meows = $meows;}function __invoke() {return str_repeat(Meow!, $this->meows);}}$furbal = new Cat(5);echo $furbal(); // Outputs: Meow!Meow!Meow!Meow!Meow!Magic method __invoke() :
  • 23. What else is new ? (3/3)class someClass {private $someProperty;function __construct($value) {$this->someProperty = $value;}public function showMeTheMoney() {return function() { echo $ . $this->someProperty; };}}$obj = new someClass(5000);;$func = $obj->showMeTheMoney();$func(); // Outputs: $5000$objA = new someClass(5000);$objB = new someClass(10000);$func = $objA->showMeTheMoney();$func(); // Outputs: $5000$func = $func->bindTo($objB);$func(); // Outputs: $100005.3 :5.4 :
  • 24. Other changesError handling :5.3 : Parse error: syntax error, unexpected T_STRING, expecting { in index.php on line 15.4 : Parse error: syntax error, unexpected bar (T_STRING), expecting { in index.php on line 1Array to string conversion :5.3 : Array5.4 : Note: Array to string conversion in test.php on line 8<?= always works (even with short_tags off)Default charset = UTF8class foo bar$var = array();echo $var;
  • 25. Time to remove !register_globalsmagic_quotessafe_modeRemoved : Still working :break $var; break 2;continue $var; continue 3;session_register()session_unregister()session_is_registered()→ use $_SESSION
  • 26. More stuff removedTimezone guessing → date.timezone in php.inisqlite extension → use sqlite3New reserved keywords :traitinsteadofcallable
  • 27. So...Should you upgrade today ?
  • 28. Upgrade : yes / noYes NoUsing removed extensions xUsing removed functions xNeed extra performance / reduced memory xReally need new feature xNo unit tests xNo package available (.rpm, .deb, ...) x
  • 29. Postponing upgradesEnd-Of-LifeIn the past : well seeNow : minor release + 2 = out → EOL5.5 = OUT → 5.3 = EOL (soon !)5.6 = OUT → 5.4 = EOL (next year !)Critical security patches : 1 yearNo bugfixesFramework supportDeveloper motivation
  • 30. So you want to upgrade...Option 1 : run your unit testsOption 2 : visit each page (good luck !) + check error_logOption 3 : automate it !
  • 31. Back in 2010...PHP Architect @ Belgian Railways8 years of legacy code40+ different developers40+ projectsChallenge :migrate all projects fromPHP 5.2.4 (on Solaris)toPHP 5.3.x (on Linux)
  • 32. The ideaAutomate itHow ? → Use the CI environmentWhich tool ? → PHP_CodeSniffer
  • 33. PHP_CodeSnifferDetect coding standard violationsSupports multiple standardsStatic analysis tool→ Runs without executing code→ Splits code in tokensEx. : T_OPEN_CURLY_BRACKETT_FALSET_SEMICOLON
  • 34. PHP_CodeSnifferLets see what it looks like
  • 35. PHPCompatibilityNew PHP_CodeSniffer standardOnly purpose : find compatibility issuesDetects :Deprecated functionsDeprecated extensionsDeprecated php.ini settings and ini_set() callsProhibited function names, class names, ……Works for PHP 5.0, 5.1, 5.2, 5.3, 5.4 and 5.5
  • 36. PHPCompatibility – making it workVia GIT :git clone git://github.com/wimg/PHPCompatibility.git PHPCompatibilityDownload from Github :http://github.com/wimg/PHPCompatibilityInstall in <pear_dir>/PHP/CodeSniffer/StandardsRun :phpcs --standard=PHPCompatibility <path>
  • 37. Important notesLarge directories → can be slow !Use --extensions=php,phtmlNo point scanning .js filesTest PHP 5.4 compatibility → need PHP 5.4 on the systemStatic analysisDoesnt run codeCan not detect every single incompatibilityProvides filename and line number
  • 38. The resultZend Framework 1.7 appPHP 5.2 : working finePHP 5.3 : fail !function goto()No 100% detection95% automation = lots of time saved !
  • 39. Questions ?
  • 40. Questions ?
  • 41. ContactTwitter @wimgtrWeb http://techblog.wimgodden.beSlides http://www.slideshare.net/wimgE-mail wim.godden@cu.bePlease...Rate my talk : http://joind.in/8643
  • 42. Thanks !Please...Rate my talk : http://joind.in/8643