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.

New Stuff In Php 5.3


Published on

An overview of new stuff in PHP 5.3 and some migration issues from earlier versions.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

New Stuff In Php 5.3

  1. 1. What's New In PHP 5.3 Chris Chubb, Code Gurus [email_address]
  2. 2. Migrating from PHP 5.2.x to PHP 5.3.x <ul><li>
  3. 3. See other guides for 4.x -> 5 or 5.1 -> 5.2
  4. 4. Some of these items are things that are new to 5.2, but of interest because 5.3 has modified their implementation. </li></ul>
  5. 5. Backward Incompatible Changes <ul><li>Object._toString() </li><ul><li>used for string casting everywhere, so you can override it, in fact you MUST if you want to use it for any place you may treat an object like a string, like in an echo DEBUG statement. Objects still can't be indices of arrays though, for that you must provide your own unique instance hash or call spl_object_hash(), even if you know it's unique. (Or fake it out with an explicit string cast: $array[(string)$object]) </li></ul><li>realpath() is now fully platform independent. </li></ul>
  6. 6. Deprecations (2) <ul><li>You must now specifically cast objects as an array to use them in the array sort functions. (Do this anyway...)
  7. 7. magic methods __get, __set, __isset, __unset, and __call must always be public and can no longer be static.
  8. 8. The __call magic method is now invoked on access to private and protected methods. (Not just public any more. Self __call) </li></ul>
  9. 9. Deprecations (2) <ul><li>Oracle extension now requires at least Oracle 10. (I think this is a bad idea, too many legacy Oracle installations)
  10. 10. 'goto' and 'namespace' are now reserved words. Do a string search in your code. More on these later </li></ul>
  11. 11. New stuff <ul><li>Many new more descriptive error messages, especially with class handling.
  12. 12. Date object moved into the core, and all supporting functions have also become objects. Timezone is inspected more for intervals, etc. </li></ul>
  13. 13. Functions with new optional parameters <ul><li>
  14. 14. Highlights: </li><ul><li>datetime can now do milliseconds in the format
  15. 15. cookie functions take &quot;httponly&quot; so they can't be accessed by XSS attacks.
  16. 16. xmlreader now lets you set the encoding directly for reading. No more stream encoding manually. </li></ul></ul>
  17. 17. New Functions <ul><li>
  18. 18. Highlights: </li><ul><li>error_get_last - Just get the last error info as an assoc array
  19. 19. Multibyte string handling functions. For you UTF-8 guys. </li></ul></ul>
  20. 20. Namespaces <ul><li>
  21. 21. Use to encapsulate classes and variables to prevent naming collisions on large projects.
  22. 22. Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword. No non-PHP code either, so don't use it with embedded PHP.
  23. 23. Sub-namespace with backslashes: namespace MyProjectSubLevel; </li></ul>
  24. 24. Namespaces (2) <ul><li>Namespaces may be single-line versions:
  25. 25. namespace ThisNamespace; //Continue to end of file or next namespace directive
  26. 26. or multi-line versions
  27. 27. namespace ThisNamespace { //Put your classes and variables in here }
  28. 28. Global namespace code may be put in a unnamed bracketed section.
  29. 29. namespace { //Global code } </li></ul>
  30. 30. Namespaces (3) <ul><li>Reference namespace using directory-like naming. Use single backslash to call a global function, even if a local function of the same name exists.
  31. 31. 'use' will import a namespace into the current namespace and the 'as' clause can be used to alias it.
  32. 32. Note: Nested namespaces are not allowed.
  33. 33. Note: Cannot create local versions of the constants NULL, TRUE or FALSE. Do not &quot;define(TRUE, 0);&quot; for you jokers out there. </li></ul>
  34. 34. Late Static Bindings <ul><li>
  35. 35. &quot;self::&quot; - Static references to the current class like self:: or __CLASS__ are resolved using the class in which the function belongs, as in where it was defined
  36. 36. &quot;static::&quot; - Replaces reference with run-time definition, so a subclass instance will use the current child class instead of the parent class. </li></ul>
  37. 37. Goto Operator === Evil <ul><li>goto a; echo 'Foo'; a: echo 'Bar';
  38. 38. Enough said. </li></ul>
  39. 39. Anonymous Functions (Closures) <ul><li>
  40. 40. Use an unnamed function as the parameter for a callback function to keep code readable and not worry about namespace collisions.
  41. 41. Can also assign an anonymous function to a variable and call like &quot;$function($param);&quot;
  42. 42. Anon functions may inherit certain variables from their parent &quot;calling&quot; stack with the &quot;use&quot; operator. </li></ul>
  43. 43. Two new &quot;magic&quot; functions <ul><li>__invoke </li><ul><li>When an object is being called like a function. </li></ul><li>__callStatic </li><ul><li>Like __call but for inaccessible static functions. </li></ul></ul>
  44. 44. NEWDOC String Declaration <ul><li>Like Heredoc, but treated like a single quoted string. No parsing. </li><ul><li>$string=<<'ENDOFSTRING' Blah &quot;costs&quot; $20.00 ENDOFSTRING; </li></ul><li>Heredoc may now be defined with double quotes. I recommend this to increase readability. </li></ul>
  45. 45. Const Declaration <ul><li>You can now use &quot;const&quot; outside of a class as a synonym of &quot;define&quot;. </li></ul>