PHP 5 Sucks. PHP 5 Rocks.

2,780 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,780
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
35
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

PHP 5 Sucks. PHP 5 Rocks.

  1. 1. PHP 5 Sucks! PHP 5 Rocks! Adam Trachtenberg eBay Technical Evangelist [email_address]
  2. 2. In the Beginning... <ul><li>SOAP Sucks! SOAP Rocks! </li></ul><ul><ul><li>Conclusion: SOAP Sucks and Rocks! </li></ul></ul><ul><li>mod_perl 2.0 Sucks! mod_perl 2.0 Rocks! </li></ul><ul><ul><li>Conclusion: mod_perl 2.0 Sucks! </li></ul></ul><ul><li>PHP 5 Sucks! PHP 5 Rocks! </li></ul><ul><ul><li>Conclusion: ??? </li></ul></ul>
  3. 3. There and Back Again <ul><li>I decide I should stop “learning” PHP 5 via the mailing list and start actually programming in it. </li></ul><ul><li>The lies begin </li></ul><ul><ul><li>“I’m a pretty good PHP 4 programmer” </li></ul></ul><ul><ul><li>“I’ve already switched from 2 to 3, and 3 to 4, so I know how to migrate versions” </li></ul></ul><ul><ul><li>“Besides, documentation is for wimps.” </li></ul></ul><ul><li>So, PHP 5 shouldn’t be too difficult to learn. </li></ul><ul><li>Right? </li></ul>
  4. 4. Come Up With a Plan <ul><li>Install PHP 5 </li></ul><ul><li>??????? </li></ul><ul><li>$$$Profit!!! </li></ul>
  5. 5. Step 1. Install PHP 5 <ul><li>Check out latest version from CVS </li></ul><ul><li>Run ./buildconf </li></ul><ul><li>Copy over my PHP 4 config.nice file </li></ul><ul><li>Run ./config.nice </li></ul>
  6. 6. Step 1. Install PHP 5: config.nice <ul><li>#! /bin/sh </li></ul><ul><li># </li></ul><ul><li># Created by configure </li></ul><ul><li>'./configure' </li></ul><ul><li>'--prefix=/home/adam' </li></ul><ul><li>'--with-zlib' </li></ul><ul><li>'--with-dom=/home/adam/' </li></ul><ul><li>&quot;$@&quot; </li></ul>
  7. 7. Step 1. Install PHP 5: config.nice <ul><li>$ ./config.nice </li></ul><ul><li>... </li></ul><ul><li>Configuring extensions </li></ul><ul><li>checking whether to enable LIBXML support... yes </li></ul><ul><li>checking libxml2 install dir... no </li></ul><ul><li>configure: error: xml2-config not found. Please check your libxml2 installation. </li></ul>
  8. 8. Step 1. Install PHP 5: config.nice <ul><li>$ ./config.nice --with-libxml-dir=/home/adam </li></ul><ul><li>... </li></ul><ul><li>Thank you for using PHP. </li></ul>
  9. 9. Step 2. ??????? <ul><li>Maybe I should run some PHP 4 scripts and see how they work under PHP 5 </li></ul><ul><li>Let’s start with a simple DOMXML program </li></ul>
  10. 10. Step 2. DOM <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><talk> </li></ul><ul><li><title>PHP 5 Rocks!</title> </li></ul><ul><li></talk> </li></ul>
  11. 11. Step 2. DOM <ul><li>$dom = domxml_new_doc('1.0'); </li></ul><ul><li>// create and append the root element, <talk> </li></ul><ul><li>$talk = $dom->append_child($dom-> </li></ul><ul><li>create_element('talk')); </li></ul><ul><li>// create and append <title> to $talk </li></ul><ul><li>$title = $talk->append_child($dom-> </li></ul><ul><li>create_element('title')); </li></ul><ul><li>// set the text node for $title </li></ul><ul><li>$title->append_child($dom-> </li></ul><ul><li>create_text_node('PHP 5 Rocks!')); </li></ul><ul><li>// print DOM document as XML </li></ul><ul><li>echo $dom->dump_mem(true); </li></ul>
  12. 12. Step 2. DOM <ul><li>Fatal error: Call to undefined function domxml_new_doc() in /home/adam/php5/dom.xml on line 2 </li></ul>
  13. 13. Step 2. DOM <ul><li>$dom = new DOMDocument ('1.0'); </li></ul><ul><li>// create and append the root element, <talk> </li></ul><ul><li>$talk = $dom->append_child($dom-> </li></ul><ul><li>create_element('talk')); </li></ul><ul><li>// create and append <title> to $talk </li></ul><ul><li>$title = $talk->append_child($dom-> </li></ul><ul><li>create_element('title')); </li></ul><ul><li>// set the text node for $title </li></ul><ul><li>$title->append_child($dom-> </li></ul><ul><li>create_text_node('PHP 5 Rocks!')); </li></ul><ul><li>// print DOM document as XML </li></ul><ul><li>echo $dom->dump_mem(true); </li></ul>
  14. 14. Step 2. DOM <ul><li>Fatal error: Call to undefined method DOMDocument::append_child() in /home/adam/php5/dom.xml on line 5 </li></ul>
  15. 15. Step 2. DOM <ul><li>$dom = new DOMDocument('1.0'); </li></ul><ul><li>// create and append the root element, <talk> </li></ul><ul><li>$talk = $dom->append C hild($dom-> </li></ul><ul><li>create E lement('talk')); </li></ul><ul><li>// create and append <title> to $talk </li></ul><ul><li>$title = $talk->append C hild($dom-> </li></ul><ul><li>create E lement('title')); </li></ul><ul><li>// set the text node for $title </li></ul><ul><li>$title->append C hild($dom-> </li></ul><ul><li>create T ext N ode('PHP 5 Rocks!')); </li></ul><ul><li>// print DOM document as XML </li></ul><ul><li>echo $dom->dump M em(true); </li></ul>
  16. 16. Step 2. DOM <ul><li>Fatal error: Call to undefined method DOMDocument::dumpMem() in /home/adam/php5/dom.xml on line 17 </li></ul>
  17. 17. Step 2. DOM <ul><li>$dom = new DOMDocument('1.0'); </li></ul><ul><li>// create and append the root element, <talk> </li></ul><ul><li>$talk = $dom->appendChild($dom-> </li></ul><ul><li>createElement('talk')); </li></ul><ul><li>// create and append <title> to $talk </li></ul><ul><li>$title = $talk->appendChild($dom-> </li></ul><ul><li>createElement('title')); </li></ul><ul><li>// set the text node for $title </li></ul><ul><li>$title->appendChild($dom-> </li></ul><ul><li>createTextNode('PHP 5 Rocks!')); </li></ul><ul><li>// print DOM document as XML </li></ul><ul><li>echo $dom-> saveXML (); </li></ul>
  18. 18. Step 2. DOM <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><talk><title>PHP 5 Rocks!</title></talk> </li></ul>
  19. 19. Step 2. DOM <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><talk> </li></ul><ul><li><title>PHP 5 Rocks!</title> </li></ul><ul><li></talk> </li></ul>
  20. 20. Step 2. DOM <ul><li>$dom = new DOMDocument('1.0'); </li></ul><ul><li>// create and append the root element, <talk> </li></ul><ul><li>$talk = $dom->appendChild($dom-> </li></ul><ul><li>createElement('talk')); </li></ul><ul><li>// create and append <title> to $talk </li></ul><ul><li>$title = $talk->appendChild($dom-> </li></ul><ul><li>createElement('title')); </li></ul><ul><li>// set the text node for $title </li></ul><ul><li>$title->appendChild($dom-> </li></ul><ul><li>createTextNode('PHP 5 Rocks!')); </li></ul><ul><li>// print DOM document as XML </li></ul><ul><li>$dom->formatOutput = true; </li></ul><ul><li>echo $dom->saveXML(); </li></ul>
  21. 21. Step 2. DOM <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><talk> </li></ul><ul><li><title>PHP 5 Rocks!</title> </li></ul><ul><li></talk> </li></ul>
  22. 22. Step 2. DOM <ul><li>$dom = new DOMDocument ('1.0'); </li></ul><ul><li>// create and append the root element, <talk> </li></ul><ul><li>$talk = $dom->append C hild ($dom-> </li></ul><ul><li>create E lement ('talk')); </li></ul><ul><li>// create and append <title> to $talk </li></ul><ul><li>$title = $talk->append C hild ($dom-> </li></ul><ul><li>create E lement ('title')); </li></ul><ul><li>// set the text node for $title </li></ul><ul><li>$title->append C hild ($dom-> </li></ul><ul><li>create T ext N ode ('PHP 5 Rocks!')); </li></ul><ul><li>// print DOM document as XML </li></ul><ul><li>$dom->formatOutput = true; </li></ul><ul><li>echo $dom-> saveXML (); </li></ul>
  23. 23. Step 2. DOM This extension is EXPERIMENTAL . The behaviour of this extension -- including the names of its functions and anything else documented about this extension -- may change without notice in a future release of PHP. Use this extension at your own risk. Warning
  24. 24. Step 2. DOM <ul><li>DOM objects are now actually objects </li></ul><ul><ul><li>Easy to integrate within OOP </li></ul></ul><ul><ul><li>Subclass them to define your own methods </li></ul></ul><ul><li>DOM methods now use studlyCaps </li></ul><ul><ul><li>Just like the rest of the world </li></ul></ul><ul><li>DOM behaves like the specification </li></ul><ul><ul><li>Properties are actually properties instead of methods </li></ul></ul>
  25. 25. Step 2. DOM <ul><li>$dom = new DOMDocument('1.0'); </li></ul><ul><li>// create and append the root element, <talk> </li></ul><ul><li>$talk = $dom->appendChild( </li></ul><ul><li>new DOMElement ('talk')); </li></ul><ul><li>// create and append <title> to $talk </li></ul><ul><li>$title = $talk->appendChild( </li></ul><ul><li>new DOMElement ('title')); </li></ul><ul><li>// set the text node for $title </li></ul><ul><li>$title->appendChild( </li></ul><ul><li>new DOMText ('PHP 5 Rocks!')); </li></ul><ul><li>// print DOM document as XML </li></ul><ul><li>echo $dom->saveXML(); </li></ul>
  26. 26. Step 2. DOM <ul><li>Faster than before </li></ul><ul><li>Doesn’t leak memory </li></ul><ul><li>Supports more DOM features </li></ul><ul><li>Plays well with other XML extensions, like XSLT, XPath, and SimpleXML </li></ul>
  27. 27. Step 2. DOM <ul><li>$talk = ' </li></ul><ul><li><talk> </li></ul><ul><li><title>PHP 5 Rocks!</title> </li></ul><ul><li></talk>'; </li></ul><ul><li>$dom = new DOMDocument; </li></ul><ul><li>$dom->preserveWhiteSpace = false; </li></ul><ul><li>$dom->loadXML($talk); </li></ul><ul><li>// print out &quot;PHP 5 Rocks!“ </li></ul><ul><li>print $dom->firstChild->firstChild-> </li></ul><ul><li>firstChild->nodeValue; </li></ul><ul><li>PHP 5 Rocks! </li></ul>
  28. 28. Step 2. DOM <ul><li>$talk = ' </li></ul><ul><li><talk> </li></ul><ul><li><title>PHP 5 Rocks!</title> </li></ul><ul><li></talk>'; </li></ul><ul><li>$simplexml = simplexml_load_string($talk); </li></ul><ul><li>// print out &quot;PHP 5 Rocks!&quot; </li></ul><ul><li>print $simplexml->title; </li></ul><ul><li>PHP 5 Rocks! </li></ul>
  29. 29. Step 2. DOM <ul><li>DOM Sucks! </li></ul><ul><li>SimpleXML Rocks! </li></ul>
  30. 30. Step 2. ??????? <ul><li>Maybe I should run some PHP 4 scripts and see how they work under PHP 5 </li></ul><ul><li>Let’s start with a simple DOMXML program </li></ul><ul><li>Well, that didn’t go as smoothly as I thought, but I can always rely on MySQL </li></ul><ul><li>After all MySQL and PHP put the MP in LA MP </li></ul><ul><li>This will definitely work! </li></ul><ul><li>Right? </li></ul>
  31. 31. Step 2. MySQL <ul><li>$db = mysql_connect($server, $user, $password); </li></ul><ul><li>mysql_select_db(&quot;users&quot;); </li></ul><ul><li>$r = mysql_query(&quot;SELECT user FROM users&quot;); </li></ul><ul><li>while ($row = mysql_fetch_assoc($r)) { </li></ul><ul><li>print $row['user']; </li></ul><ul><li>} </li></ul><ul><li>mysql_close(); </li></ul>
  32. 32. Step 2. MySQL <ul><li>Fatal error: Call to undefined function mysql_connect() in /home/adam/php5/mysql.php on line 3 </li></ul>
  33. 33. Step 2. MySQL <ul><li>MySQL client libraries are no longer bundled with PHP! </li></ul><ul><li>You must download and install them yourselves. </li></ul>
  34. 34. Step 2. MySQL <ul><li>While I’m at it, I might as well upgrade to MySQL 4.1. </li></ul>
  35. 35. Step 2. MySQL <ul><li>However, MySQL 4.1 uses a new client protocol... </li></ul><ul><li>Turns out, there’s a new MySQL extension dedicated to MySQL 4.1 and greater </li></ul><ul><li>MySQLi </li></ul><ul><li>The “i” stands for improved </li></ul>
  36. 36. Step 2. MySQL <ul><li>$db = mysql_connect($server, $user, $password); </li></ul><ul><li>mysql_select_db(&quot;users&quot;); </li></ul><ul><li>$r = mysql_query(&quot;SELECT user FROM users&quot;); </li></ul><ul><li>while ($row = mysql_fetch_assoc($r)) { </li></ul><ul><li>print $row['user']; </li></ul><ul><li>} </li></ul><ul><li>mysql_close(); </li></ul>
  37. 37. Step 2. MySQL <ul><li>$db = mysql i _connect($server, $user, $password); </li></ul><ul><li>mysql i _select_db(&quot;users&quot;); </li></ul><ul><li>$r = mysql i _query(&quot;SELECT user FROM users&quot;); </li></ul><ul><li>while ($row = mysql i _fetch_assoc($r)) { </li></ul><ul><li>print $row['user']; </li></ul><ul><li>} </li></ul><ul><li>mysql i _close(); </li></ul>
  38. 38. Step 2. MySQL <ul><li>Warning: Missing argument 2 for mysqli_select_db() in /home/adam/php5/mysql.php on line 7 </li></ul>
  39. 39. Step 2. MySQL <ul><li>$db = mysqli_connect($server, $user, $password); </li></ul><ul><li>mysqli_select_db( $db, &quot;users&quot;); </li></ul><ul><li>$r = mysqli_query( $db, &quot;SELECT user FROM users&quot;); </li></ul><ul><li>while ($row = mysqli_fetch_assoc($r)) { </li></ul><ul><li>print $row['user']; </li></ul><ul><li>} </li></ul><ul><li>mysqli_close( $db ); </li></ul>
  40. 40. Step 2. MySQL <ul><li>$db = mysqli_connect($server, $user, $password , &quot;users&quot; ); </li></ul><ul><li>$r = mysqli_query(&quot;SELECT user FROM users&quot;); </li></ul><ul><li>while ($row = mysqli_fetch_assoc($r)) { </li></ul><ul><li>print $row['user']; </li></ul><ul><li>} </li></ul><ul><li>mysqli_close(); </li></ul>
  41. 41. Step 2. MySQL <ul><li>You now need to always pass a database handle to MySQL functions </li></ul><ul><li>Also, some older functions have been removed </li></ul><ul><li>So, I compiled a list of functions that have been changed or deleted between the MySQL and MySQLi extension </li></ul>
  42. 42. Step 2: MySQL <ul><li>mysql_connect([string hostname[:port][:/path/to/socket] [, string username [, string password [, bool new [, int flags]]]]]) </li></ul><ul><li>mysql_select_db(string database_name [, int link_identifier]) </li></ul><ul><li>mysql_query(string query [, int link_identifier]) </li></ul><ul><li>mysql_fetch_field(resource result [, int field_offset]) </li></ul><ul><li>mysql_change_user(string user, string password [, string database [, resource link_identifier]]) </li></ul><ul><li>mysql_real_escape_string(string to_be_escaped [, int link_identifier]) </li></ul><ul><li>mysql_affected_rows([int link_identifier]) </li></ul><ul><li>mysql_close([int link_identifier]) </li></ul><ul><li>mysql_error([int link_identifier]) </li></ul><ul><li>mysql_errno([int link_identifier]) </li></ul><ul><li>mysql_get_host_info([int link_identifier]) </li></ul><ul><li>mysql_get_proto_info([int link_identifier]) </li></ul><ul><li>mysql_get_server_info([int link_identifier]) </li></ul><ul><li>mysql_info([int link_identifier]) </li></ul><ul><li>mysql_insert_id([int link_identifier]) </li></ul><ul><li>mysql_ping([int link_identifier]) </li></ul><ul><li>mysql_stat([int link_identifier]) </li></ul><ul><li>mysql_thread_id([int link_identifier]) </li></ul><ul><li>mysql_create_db($database, $db); </li></ul><ul><li>mysql_drop_db($database, $db); </li></ul><ul><li>mysql_escape_string($string); </li></ul><ul><li>mysql_field_flags($result, $i); </li></ul><ul><li>mysql_field_len($result, $i) </li></ul><ul><li>mysql_field_name($result, $i) </li></ul><ul><li>mysql_field_table($result, $i) </li></ul><ul><li>mysql_field_type($result, $i) </li></ul><ul><li>mysql_db_name($result, $i) </li></ul><ul><li>mysql_db_query($database, $query, $db); </li></ul><ul><li>mysql_list_dbs($db); </li></ul><ul><li>mysql_list_fields($database, $table, $db) </li></ul><ul><li>mysql_list_processes($db) </li></ul><ul><li>mysql_list_tables($database, $db) </li></ul><ul><li>mysql_pconnect($hostname, $username, $password); </li></ul><ul><li>mysql_result($result, $column); </li></ul><ul><li>mysql_tablename($result, $i) </li></ul><ul><li>mysql_unbuffered_query() </li></ul>
  43. 43. Step 2. MySQL <ul><li>Object-oriented interface </li></ul><ul><li>Prepared statements </li></ul><ul><li>Bound input and output parameters </li></ul><ul><li>SSL connections </li></ul><ul><li>Multi-query function </li></ul><ul><li>Subselects </li></ul><ul><li>Internationalization </li></ul><ul><li>Whole lots more! </li></ul><ul><li>This rocks! </li></ul>
  44. 44. Step 2. MySQL <ul><li>$db = new mysqli($server, $user, $password, &quot;users&quot;); </li></ul><ul><li>$r = mysqli->query(&quot;SELECT user FROM users&quot;); </li></ul><ul><li>while ($row = $r->fetch_assoc()) { </li></ul><ul><li>print $row['user']; </li></ul><ul><li>} </li></ul><ul><li>unset($db); </li></ul>
  45. 45. Step 2. MySQL <ul><li>$db = new mysqli($server, $username, $password, &quot;users&quot;); </li></ul><ul><li>$stmt = $db->stmt_init(); </li></ul><ul><li>if ($stmt->prepare(&quot;SELECT user FROM users&quot;)) { </li></ul><ul><li>$stmt->bind_result($user); </li></ul><ul><li>$stmt->execute(); </li></ul><ul><li>while ($stmt->fetch()) { </li></ul><ul><li>print $user; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>unset($db); </li></ul>
  46. 46. Step 3. $$$Profit!!! <ul><li>The good news is that I’ve shown you (almost) all the sucky parts of PHP 5. </li></ul><ul><li>Everything else rocks! </li></ul>
  47. 47. Step 3. $$$Profit!!! <ul><li>Object-oriented programming </li></ul><ul><ul><li>Constructors </li></ul></ul><ul><ul><li>Destructors </li></ul></ul><ul><ul><li>Public, protected, and private properties and methods </li></ul></ul><ul><ul><li>Interfaces </li></ul></ul><ul><ul><li>Abstract classes </li></ul></ul><ul><ul><li>Class type hints </li></ul></ul><ul><ul><li>Static properties and methods </li></ul></ul><ul><ul><li>Final properties and methods </li></ul></ul><ul><ul><li>A whole suite of magical methods </li></ul></ul><ul><ul><li>Proper references </li></ul></ul>
  48. 48. Step 3. $$$Profit!!! <ul><li>Object-oriented programming </li></ul><ul><li>XML </li></ul><ul><ul><li>DOM </li></ul></ul><ul><ul><li>SimpleXML </li></ul></ul><ul><ul><li>XSLT </li></ul></ul><ul><ul><li>XPath </li></ul></ul><ul><ul><li>SAX </li></ul></ul><ul><ul><li>XML Pull Parser </li></ul></ul>
  49. 49. Step 3. $$$Profit!!! <ul><li>Object-oriented programming </li></ul><ul><li>XML </li></ul><ul><li>SQLite </li></ul><ul><ul><li>Embedded database library </li></ul></ul><ul><ul><li>Guarantees access to database in PHP 5 </li></ul></ul><ul><ul><li>Not “Lite” at all: </li></ul></ul><ul><ul><ul><li>Transactions </li></ul></ul></ul><ul><ul><ul><li>Triggers </li></ul></ul></ul><ul><ul><ul><li>Subselects </li></ul></ul></ul>
  50. 50. Step 3. $$$Profit!!! <ul><li>Object-oriented programming </li></ul><ul><li>XML </li></ul><ul><li>SQLite </li></ul><ul><li>Iterators </li></ul><ul><li>Error handling with exceptions </li></ul><ul><li>SOAP extension </li></ul><ul><li>Streams, wrappers, and filters </li></ul><ul><li>Reflection classes </li></ul><ul><li>Better command line processing </li></ul><ul><li>Compliant HTML from Tidy </li></ul>
  51. 51. In the Beginning... <ul><li>SOAP Sucks! SOAP Rocks! </li></ul><ul><ul><li>Conclusion: SOAP Sucks and Rocks! </li></ul></ul><ul><li>mod_perl 2.0 Sucks! mod_perl 2.0 Rocks! </li></ul><ul><ul><li>Conclusion: mod_perl 2.0 Sucks! </li></ul></ul><ul><li>PHP 5 Sucks! PHP 5 Rocks! </li></ul><ul><ul><li>Conclusion: PHP 5 Rocks! </li></ul></ul>
  52. 52. $$$Profit!!! Shameless Plug

×