New Features in PHP 5.3

64,531 views

Published on

PHP 5.3 was released in 2009 and is the newest version of PHP. Bradley Holt will talk about the new features available including namespaces, closures (lamdba/anonymous functions), and late static binding. Learn how these new features might be useful to you and why the next major version of the big PHP frameworks will require PHP 5.3.

Published in: Technology
0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
64,531
On SlideShare
0
From Embeds
0
Number of Embeds
1,038
Actions
Shares
0
Downloads
147
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide

New Features in PHP 5.3

  1. 1. New Features in PHP 5.3 Bradley Holt (http://bradley-holt.com/)
  2. 2. Introduction
  3. 3. Are you using PHP 5.3?
  4. 4. Past, Present & Future PHP 5.0 brought us a new object model PHP 5.3 brings us namespaces, closures, late static binding & more PHP 5.3.99—huh?
  5. 5. So What? Speed & memory improvements Some problems are easier to solve in PHP 5.3 Zend Framework 2.0, Symfony 2.0, Lithium & Doctrine 2.0 will require PHP 5.3
  6. 6. New Features
  7. 7. Namespaces, Late Static Binding & Closures
  8. 8. Namespaces
  9. 9. Namespaced Classes <?php namespace btvphpstuff; class Foo {} $a = new btvphpstuffFoo(); $b = new Foo(); // Same as above
  10. 10. Namespaced Functions <?php namespace btvphpstuff; function sayHi() { return'Hi'; } echo btvphpstuffsayHi(); // Hi echo sayHi(); // Hi
  11. 11. Namespace Aliasing Examples courtesy of Matthew Weier O’Phinney (http://weierophinney.net/matthew/)
  12. 12. Aliasing Classes <?php namespace ZendSignalSlot { class Signals {} } namespace { // global namespace use ZendSignalSlotSignals; $signals = new Signals(); }
  13. 13. Changing the Name <?php namespace ZendLoader { class PluginBroker {} } namespace { use ZendLoaderPluginBroker as Broker; $broker = new Broker(); }
  14. 14. Global Resolution <?php namespace Doctrine { class Manager { public static function load() {} } } namespace { DoctrineManager::load(); }
  15. 15. Late Static Binding
  16. 16. The Problem
  17. 17. Parent Class <?php class Foo { protected static function speak() { return 'Hi'; } public static function sayHi() { return self::speak(); } }
  18. 18. Child Class <?php class Bar extends Foo { protected static function speak() { return 'Hello'; } }
  19. 19. “Hi”or“Hello”? <?php echo Bar::sayHi();
  20. 20. “Hi”or“Hello”? <?php echo Bar::sayHi(); // Hi
  21. 21. The Solution
  22. 22. Parent Class <?php class Foo { protected static function speak() { return 'Hi'; } public static function sayHi() { return static::speak(); } }
  23. 23. Child Class <?php class Bar extends Foo { protected static function speak() { return 'Hello'; } }
  24. 24. “Hi”or“Hello”? <?php echo Bar::sayHi(); // Hello
  25. 25. Closures / Lambda Functions See: http://bit.ly/9LYP3r
  26. 26. Variable Assignment <?php $sayHi = function () { return 'Hi'; }; echo $sayHi(); // Hi
  27. 27. Scope <?php $sayWhat = 'Hi'; $say = function ($toWhom) use ($sayWhat) { return $sayWhat . ', ' . $toWhom; }; echo $say('Bradley'); // Hi, Bradley
  28. 28. Anonymous Functions <?php $values = array(3, 7, 2); usort($values, function ($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }); /* [0] => 2 [1] => 3 [2] => 7 */
  29. 29. Other Neat Stuff
  30. 30. New Bundled Extensions Phar (PHP Archive) Internationalization Functions Fileinfo: guesses content type & encoding SQLite version 3 Enchant spelling library
  31. 31. Extension Improvements
  32. 32. OpenSSL More OpenSSL functionality available natively within PHP Faster than do-it-yourself or system calls Useful if you’re working with OpenID, etc.
  33. 33. DateTime Object Add or subtract date intervals Calculate the difference between two dates Get or set unix timestamp See: http://bit.ly/5pDpWI
  34. 34. SPL Data Structures See: http://bit.ly/bz6pqY
  35. 35. SplStack Push & Pop Last In, First Out (LIFO) Uses less memory than arrays for big stacks (greater than 5,000 elements)
  36. 36. SplQueue Enqueue & Dequeue First In, First Out (FIFO) Faster and uses less memory than arrays for most queues
  37. 37. SplHeap Insert & Remove Reorders elements based on comparisons Faster and uses less memory than arrays for most heaps
  38. 38. goto Courtesy of xkcd: http://xkcd.com/292/
  39. 39. New Syntax
  40. 40. __invoke() <?php class Foo { public function __invoke($x) { return $x + $x; } } $foo = new Foo(); echo $foo(2); // 4
  41. 41. __callStatic() <?php class Foo { public static function __callStatic ($name, $args) { return $name . ' called statically'; } } echo Foo::bar(); // bar called statically
  42. 42. __DIR__ <?php echo dirname(__FILE__); echo __DIR__; // Since PHP 5.3
  43. 43. Miscellaneous Nowdocs:“Nowdocs are to single-quoted strings what heredocs are to double-quoted strings.” Improved ternary (?:) operator
  44. 44. I’m Sold, What Now?
  45. 45. Platform Support
  46. 46. Linux Ubuntu 10.10 Fedora 12+ openSuse 11.2+ Red Hat Enterprise Linux (RHEL) 6
  47. 47. Mac OS X Bundled with Snow Leopard MacPorts Homebrew
  48. 48. Windows Binary packages available WebMatrix Beta 3
  49. 49. PHP 5.3 Hosting ServerGrove WebMatrix A2 Hosting Hostek
  50. 50. Resources PHP Manual http://php.net/ PHP 5.3.0 Release Announcement http://php.net/releases/5_3_0.php Migrating from PHP 5.2.x to PHP 5.3.x http://www.php.net/manual/en/migration53.php
  51. 51. Questions?
  52. 52. Thank You Bradley Holt (http://bradley-holt.com/)
  53. 53. New Features in PHP 5.3 by Bradley Holt is licensed under a Creative Commons Attribution 3.0 Unported License. License

×