PHP 5.4 - Begin your love affair with traits

16,965 views
16,800 views

Published on

Slides for my talk at OSDC PHP MiniConf. November 14, 2011.

Published in: Technology
5 Comments
24 Likes
Statistics
Notes
No Downloads
Views
Total views
16,965
On SlideShare
0
From Embeds
0
Number of Embeds
6,749
Actions
Shares
0
Downloads
250
Comments
5
Likes
24
Embeds 0
No embeds

No notes for slide

PHP 5.4 - Begin your love affair with traits

  1. 1. PHP 5.4Begin your love affair with Traits- Graham Weldon
  2. 2. Graham WeldonPHP developer since... foreverCakePHP contributor/ evangelistSerial conference speakerGaming enthusiastBeer drinkerhttp://grahamweldon.comhttp://cakedc.comhttp://cakephp.orgTwitter: @predominant
  3. 3. SydPHPWe’ve got awesome stuff coming upVisit us! http://sydphp.orgNovember MeetupDecember “Phunconference 2”January Australia Day BBQ
  4. 4. What PHP Version? PHP 4 PHP 5.1 PHP 5.2 PHP 5.3 PHP 5.4 PHP 6 (Yes, there are some people using it)
  5. 5. 100 75 50 25PHP 3 PHP 4 0 PHP 5 PHP 6 Data courtesy of w3techs.com
  6. 6. 80 60 40 205.0 5.1 5.2 0 5.3 5.4 Data courtesy of w3techs.com
  7. 7. Traditionally slow adopters PHP 4 to PHP 5 was a painfully slow upgrade for the global community Shared hosts retained old versions Object handling was different PHP 5.1 on CentOS People don’t upgrade stable systems PHP 5.2 massive exposure Again, people don’t upgrade stable systems PHP 5.3 used, but not fully
  8. 8. Why slow adopters? Shared hosts Established applications Upgrade path Old excuses
  9. 9. Why adopt early? New language features More elegant ways to solve problems Cleaner code Adopting early gets you noticed
  10. 10. What about instability? Be sensible with production-ready systems Don’t use the bleeding edge Confirm functionality Unit Testing Integration testing Load testing If your tests pass, then phase in the new system Use sensible reporting / monitoring
  11. 11. Enter PHP 5.4 (RC1)
  12. 12. PHP 5.4-RC1 RC1 was released for QA on 11th November 2011 Some new features, plenty of bug fixes You can download it for yourself! http://qa.php.net/ Give it a try See if your applications run on it
  13. 13. PHP 5.4New Language Features and Improvements
  14. 14. Upload Progress
  15. 15. Binary Notation!
  16. 16. Array short syntaxhttps://wiki.php.net/rfc/shortsyntaxforarrays
  17. 17. Array dereferencinghttps://wiki.php.net/rfc/functionarraydereferencing
  18. 18. Class member access on instantiation
  19. 19. Closures (Current)
  20. 20. Closures (Array)https://wiki.php.net/rfc/indirect-method-call-by-array-var
  21. 21. But this doesn’t work... :-(
  22. 22. Closures and ‘$this’https://wiki.php.net/rfc/closures/object-extension
  23. 23. Callable Typehinthttps://wiki.php.net/rfc/callable
  24. 24. JsonSerializable Interface
  25. 25. Short Tag echo - Always available
  26. 26. Oh, and there is a built-inweb server now. Yay?
  27. 27. https://wiki.php.net/
  28. 28. PHP Traits
  29. 29. Whats the big deal? Elegant code re-use Without complicated multiple inheritance No diamond issues Kinda like interfaces But provides implementation Other languages are doing similar things PHP is keeping up with the trends
  30. 30. A look atGolang interfaces
  31. 31. PHP’s approach Traits define small sections of reusable code This code can be included in any class There is a loading precedence order Puts developers in control Looks a feels natural No need to write things twice Override capabilities
  32. 32. PHP TraitsExample
  33. 33. PHP TraitsExample
  34. 34. PHP TraitsReal Example
  35. 35. SometimesTraits needAdjustmentMaybe log() needs tobe public in youruse case.
  36. 36. ChangeMethodsas you likelog() is now accessibleto the User instancein this context.
  37. 37. CustomiseFurtherChange method namesif you’re into thatsort of thing.Provide preferences.
  38. 38. Graham Weldon Twitter: @predominant http://grahamweldon.com Its over! http://cakedc.comQuestions? http://cakephp.org Get in touch if you want to know more about this presentation, PHP in general, or anything. :-)

×